python 核心编程课后练习(chapter 3)
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)的更多相关文章
- python 核心编程课后练习(chapter 6)
6-1 #6-1 #help(string) import string str = "helloworld" substr = "h1e" if string ...
- 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< ...
- python 核心编程课后练习(chapter 2)
2-4 #2-4(a) print "enter a string" inputstring = raw_input() print"the string is: &qu ...
- Python核心编程课后习题-第六章
1. 字符串, string模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? str1 = 'abcdefghijklmnopqrstuv' print st ...
- Python 核心编程 课后习题 第五章
2. 操作符. (a) 写一个函数, 计算并返回两个数的乘积. (b) 写一段代码调用这个函数, 并显示它的结果. def multi(a,b): return a * b result = mult ...
- Python核心编程 课后练习 第二章
2.4 使用raw_input()函数得到用户输入. (a) 创建一段脚本使用raw_input()函数从用户输入得到一个字符串, 然后显示这个用户杠杠输入的字符串. #coding = utf-8 ...
- python核心编程(第二版)习题
重新再看一遍python核心编程,把后面的习题都做一下.
- Python核心编程这本书的一些错误
<Python核心编程第二版>这本书比<Python基础教程第二版修订版>详细很多,丰富了很多细节,虽然它是一本经典的入门书,但我发现还是存在一些明显的错误.在面向对象编程这一 ...
- Python核心编程-描述符
python中,什么描述符.描述符就是实现了"__get__"."__set__"或"__delete__" 方法中至少一个的对象.什么是非 ...
随机推荐
- 未能加载文件或程序集“AspNetPager”或它的某一个依赖项。参数错误(转)
未能加载文件或程序集“AspNetPager”或它的某一个依赖项.参数错误. 看你的的开发框架用的是多少的2.0, 3.0, 3.5, 4.0 那么删除的框架的文件夹也相对应的变化 删除 C:\W ...
- java内存图解
最近突然对java的内存管理来了兴趣想,之前对java内存很模糊,通过这几天的学习现在将学习成果记录下来欢迎大家多多指教,为了方便我自己画了一个图
- 自己瞎捣腾的Win7下Linux安装之路-----图例篇
写在前面 网上很多这方面的教程,我也是参考他们的文章和一些书籍才弄成功.没啥创新性,只为记录下来,以供自已理解和以后复习. 所谓工欲善其事,必先利其器.俺想学习Linux,当然是要先装一个Linux( ...
- 将网页另存为PDF文件的方法
使用google chrome浏览器测试,其他浏览器应该也是差不多的方法. 步骤1: 打开需要转换的网页: 步骤2: 点击右上角的三点按键,或者快捷键Ctrl+P,调用的打印页面: 步骤3: 选择目标 ...
- 常用的shell命令
作为一名有10年以上使用年龄的Linux/MacOSX用户来说,键盘的作用往往要远大于鼠标.而且,作为一个黑客文化的追随者,我也希望越来越多的用户能认识到命令行的潜在价值,不要把它视为只是计算机专家们 ...
- python logging模块详解[转]
一.简单将日志打印到屏幕: import logging logging.debug('debug message') logging.info('info message') logging.war ...
- 记一次ifconfig命令
由于Windows 10的强制更新,原来的Virtual box Host-Only驱动莫名奇妙的不见了,于是上网找各种解决方案: 1.重新生成虚拟网卡适配器:执行 VBoxManage.exe ho ...
- 依赖注入 – ASP.NET MVC 4 系列
从 ASP.NET MVC 3.0 开始就引入了一个新概念:依赖解析器(dependence resolver).极大的增强了应用程序参与依赖注入的能力,更好的在 MVC 使用的服务和创 ...
- linux 几个控制流语句的格式例子(if语句)
linux 几个控制流语句的格式例子:if 语句例子:#!/bin/sh a=10b=20 if [ $a == $b ]then echo "a is equal to b"el ...
- MS SQL执行大脚本文件时,提示“内存不足”的解决办法()
问题描述: 当客户服务器不允许直接备份时,往往通过导出数据库脚本的方式来部署-还原数据库, 但是当数据库导出脚本很大,用Microsoft SQL Server Management Studio执行 ...