3-8

 #3-8
"makeTextFile.py -- create text file" import os
ls = os.linesep #get filename
fname = raw_input()
while True: if os.path.exists(fname):
print "ERROR: '%s' already exists" % fname
else:
break
all = []
print "\nEnter lines ('.' by itself to quit).\n" while True:
entry = raw_input('newline:')
if entry == '.':
break
else:
all.append(entry) fobj = open(fname, 'w')
fobj.writelines(['%s%s' % (x, ls) for x in all])
fobj.close()
print "DONE!"

3-10

 #3-10
"readtextFile.py---read and display text"
import os fname = raw_input("Enter file name:")
print if os.path.exists(fname):
fobj = open(fname, 'r')
for eachline in fobj:
print eachline.strip()
fobj.close()
else:
print "file not exists!!" #3-11
"readtextFile.py---read and display text" fname = raw_input("Enter file name:")
print try:
fobj = open(fname, 'r')
except IOError, e:
print "*** file open error: ", e
else:
for eachline in fobj:
print eachline.strip()
fobj.close()

3-12

 #3-12
"readtextFile.py---read and display text"
import os
ls = os.linesep print """Enter your option:
(w)create a file
(r)read a file
(others)exit
""" def WriteFile(str):
all = []
if os.path.exists(str):
print "ERROR: '%s' already exists" % fname
return print "\nEnter lines ('.' by itself to quit).\n"
while True:
entry = raw_input('newline:')
if entry == '.':
break
else:
all.append(entry) fobj = open(fname, 'w')
fobj.writelines(['%s%s' % (x, ls) for x in all])
fobj.close()
print "DONE!" def ReadFile(str):
#fname = raw_input("Enter file name:")
print if os.path.exists(str):
fobj = open(str, 'r')
for eachline in fobj:
print eachline.strip()
fobj.close()
else:
print "file not exists!!" opt =raw_input("your option is:") if(opt == 'w'):
fname = raw_input("the file to write is: ")
WriteFile(fname)
elif opt== 'r':
fname = raw_input("the file to read is: ")
ReadFile(fname)
else:
print "exit"

3-13

 #3-13
"readtextFile.py---read and display text"
import os
ls = os.linesep print """Enter your option:
(w)create a file
(r)read a file
(m)modify a file
(others)exit
""" def WriteFile(str):
all = []
if os.path.exists(str):
print "ERROR: '%s' already exists" % fname
return print "\nEnter lines ('.' by itself to quit).\n"
while True:
entry = raw_input('newline:')
if entry == '.':
break
else:
all.append(entry) fobj = open(fname, 'w')
fobj.writelines(['%s%s' % (x, ls) for x in all])
fobj.close()
print "DONE!" def ReadFile(str):
#fname = raw_input("Enter file name:")
print if os.path.exists(str):
fobj = open(str, 'r')
for eachline in fobj:
print eachline.strip()
fobj.close()
else:
print "file not exists!!" def ModifyFile(str):
all = []
if os.path.exists(str):
i = 1
fobj = open(str, 'r')
for eachline in fobj:
newline = eachline.strip()
print "line %d: %s" % (i,newline)
newline = raw_input("replace with:")
all.append(newline)
i+=1
fobj.close() opt = raw_input("do you want to save your changes,\ny(yes) \nother(n):")
if opt == 'y':
output = open(str, 'w')
output.writelines(['%s%s' % (x, ls) for x in all])
output.close()
else:
return
else:
print "file not exists!!" opt =raw_input("your option is:") if(opt == 'w'):
fname = raw_input("the file to write is: ")
WriteFile(fname)
elif opt== 'r':
fname = raw_input("the file to read is: ")
ReadFile(fname)
elif opt == 'm':
fname = raw_input("the file to modify is:")
ModifyFile(fname)
print "the new file content is:"
ReadFile(fname)
else:
print "exit"

最新文章

  1. IOS 开发小技巧总结
  2. Linux vi/vim(转载)
  3. centos6.8服务器部署svn
  4. jQuery+fullPage.js演示10种全屏滚动
  5. 第16章 调色板管理器_16.4 一个DIB位图库的实现(2)
  6. PHP笔记(PHP高级篇)
  7. JS加载时间线
  8. poj 1017 Packets 裸贪心
  9. QT断点续传
  10. 总结html5-canvas学习笔记
  11. Python小杂点
  12. Lua的安装
  13. spring与hibernate整合事务管理的理解
  14. C#后台创建控件并获取值
  15. C++输入输出进制、数据宽度与对齐、精度、取整
  16. JS菜单条智能定位效果
  17. Python 查找binlog文件
  18. 史上最全PMP备考考点全攻略(上篇-五大过程组,附赠资料)
  19. #Java学习之路——基础阶段(第三篇)
  20. IDEA+Springboot+JRebel热部署实现

热门文章

  1. 一些新的ideas
  2. ecshop后台分页浅析
  3. 向内存0:200~0:23f依次传送数据0~3fh
  4. iOS NSTimer使用详解 开启、关闭、移除
  5. Windows 2012 中文乱码的解决办法
  6. asp.net环境搭建
  7. rails: 的cookie小结
  8. MyBatis入门学习教程-Mybatis3.x与Spring4.x整合
  9. windows下docker环境设置
  10. SQL 递归树 子父节点相互查询