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"

python 核心编程课后练习(chapter 3)的更多相关文章

  1. python 核心编程课后练习(chapter 6)

    6-1 #6-1 #help(string) import string str = "helloworld" substr = "h1e" if string ...

  2. python 核心编程课后练习(chapter 5)

    5-2 #5-2 def mul(x, y): return x * y print mul(4,5) 5-3 #5-3 def value_score(num): if 90<=num< ...

  3. python 核心编程课后练习(chapter 2)

    2-4 #2-4(a) print "enter a string" inputstring = raw_input() print"the string is: &qu ...

  4. Python核心编程课后习题-第六章

    1. 字符串, string模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? str1 = 'abcdefghijklmnopqrstuv' print st ...

  5. Python 核心编程 课后习题 第五章

    2. 操作符. (a) 写一个函数, 计算并返回两个数的乘积. (b) 写一段代码调用这个函数, 并显示它的结果. def multi(a,b): return a * b result = mult ...

  6. Python核心编程 课后练习 第二章

    2.4 使用raw_input()函数得到用户输入. (a) 创建一段脚本使用raw_input()函数从用户输入得到一个字符串, 然后显示这个用户杠杠输入的字符串. #coding = utf-8 ...

  7. python核心编程(第二版)习题

    重新再看一遍python核心编程,把后面的习题都做一下.

  8. Python核心编程这本书的一些错误

    <Python核心编程第二版>这本书比<Python基础教程第二版修订版>详细很多,丰富了很多细节,虽然它是一本经典的入门书,但我发现还是存在一些明显的错误.在面向对象编程这一 ...

  9. Python核心编程-描述符

    python中,什么描述符.描述符就是实现了"__get__"."__set__"或"__delete__" 方法中至少一个的对象.什么是非 ...

随机推荐

  1. iOS 系统架构

    https://developer.apple.com/library/ios/documentation/Miscellaneous/Conceptual/iPhoneOSTechOverview/ ...

  2. iptables的扩展匹配

    iptables的匹配条件 一.通用匹配:-s.-d.-p.-i.-o 二.扩展匹配 1.隐含扩展:使用-p{tcp|udp|icmp}指定某特定协议后,自动能够对协议进行扩展 -p tcp --dp ...

  3. json loads No JSON object could be decoded 问题解决

    今天自己用json的 dumps生成的 json 文本: f2=open(filename,'w')f2.write(json.dumps(c) f=open(filename,'r')r= f.re ...

  4. 分享一个ruby网站 | 菜鸟教程

    http://www.runoob.com/ruby/ruby-tutorial.html 分享一个ruby网站.

  5. maven加载jar包配置

    maven build时报程序包不存在和找不到符号的错误,但是代码中不报错,如下: [ERROR] Failed to execute goal org.apache.maven.plugins:ma ...

  6. jQuery中的map()方法

    jQuery中map()方法的使用格式为:$(selector).map(callback(index,domElement)). 将在每一个被选元素上执行map()方法中设置的回调函数,在回调函数中 ...

  7. [java]wordcount程序

    词数统计系统. 作业解析:这次作业的内容是从本地读取一个程序代码,计算出这个程序中的行数,单词数,也可进行拓展. 实现语言:java 编程思路: 程序是由各种单词和符号组成的,单词包括关键字,标识符这 ...

  8. C++ 中静态成员函数访问非静态成员变量的方法

    最近在 VS2010 里开发出厂烧写工具,遇到一个问题: 我创建了一个线程,在这个线程里要访问非静态成员,而这个线程函数是静态的.最后找到的办法是用对象指针来做. sourcecode: #test. ...

  9. .net 创建计划任务开机后自动以管理员身份启动运行 win7 ~ win10

    假如要启动 this.exe.以下逻辑中会启动先后关联启动三个实例分别是ABC.先启动第一个实例A,A启动实例B,B启动实例C. 要求: 1.如果没有以管理员权限运行,则请求管理员权限运行,即使没有请 ...

  10. windows开机记录查询

    http://jingyan.baidu.com/article/3d69c5516b9a9ef0cf02d7f3.html