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. bootstrap-16

    进度条----基本样式: Bootstrap框架中对于进度条提供了一个基本的样式,一个100%宽度的背景色,然后高亮颜色表示完成进度.其实制作这样的进度条非常容易,一般是使用两个容器,外容器具有一定的 ...

  2. ecshop后台分页浅析

      既然是分页,道理都是一样的,不过ecshop前台分页的函数和后台分页的函数不同,后台分页函数为page_and_size(),在admin/includes/lib_main.php里.都是用aj ...

  3. wince5代码整理

    BAT文件语法: @REM 这是注释标识与REM的区别就是在echo on时REM的注释也会显示出来 @REM 设置变量BSP_SMDK2416为2 set BSP_SMDK2416=2 @REM 设 ...

  4. Windows快速删除文件脚本

    1.新建一个txt文件 2.将DEL /F /A /Q \\?\%1RD /S /Q \\?\%1这段代码放在新建好的txt文件中 3.将txt文件的后缀名改为.bat 4.将这个文件放在需要删除的文 ...

  5. C#防止反编译

    http://blog.csdn.net/wangpei421/article/details/42393095 http://www.cnblogs.com/tianguook/archive/20 ...

  6. ProcessOn

    1.地址:http://www.processon.com/ 2.简介:在线创作流程图.BPMN.UML图.UI界面原型设计.iOS界面原型设计等. 3.优势:无需安装,简单易用.可以替代VISO,学 ...

  7. Head First 设计模式之观察者模式(Observer Pattern)

    前言: 这一节开始学习观察者模式,开始讲之前会先像第一节那样通过一个应用场景来引入该模式.具体场景为:气象站提供了一个WeatherData对象,该对象可以追踪获取天气的温度.气压.湿度信息,Weat ...

  8. 打开页面自动打开QQ的javascript代码

    今天浏览个网站,发现我的QQ突然自动启动了,起初还以为中病毒了,后来找了半天无果,反而发现了几个无需启动的系统服务进程,遂管之. 后来打开网站的页面的源代码,发现一段javascript脚本,才知道我 ...

  9. [综]前景检测GMM

    tornadomeet 前景检测算法_4(opencv自带GMM) http://www.cnblogs.com/tornadomeet/archive/2012/06/02/2531705.html ...

  10. AngularJS---表达式

    AngularJS的表达式是放在{{}}里面,用{{ }}符号将一个变量绑定到$scope上. angularJS中的表达式有如下特点: 1.只能在其所属作用域内部 所有的表达式都在其所属的作用域内部 ...