python学习日记_第七天(ex16~17)

L16读写文件

1.通过赋予open() ‘w’参数来写入内容到文件中。


#coding:utf-8
#习题 16: 读写文件
#备注:执行时遇到问题,执行完毕后发现文件没有被修改。找到原因是因为:
#***没有给出txt的绝对路径时,文件会在当前目录下生成,而不是我希望的L16的文件夹下。要注意。

from sys import argv

script, filename = argv

print "We're going to erase %r." % filename
#CTRL-C可以用来退出执行中的脚本
print "If you don't want that, hit CTRL-C (^C)."
print "If you do want that, hit RETURN."

#这边用raw_input作为一个中断程序执行的作用。
raw_input("?")

print "Opening the file..."

#open的参数为"w","r,"a","U",用了大写W报错
#target = open(filename, 'W')
target = open(filename,'w')

#因为使用了open的w模式,下面的truncate命令其实是多余的。
#因为本身就会消除原来文件内的内容。简化代码,注释掉。
#print "Truncating the file. Goodbye!"
#target.truncate()

#让用户输入三个句子
print "Now I'm going to ask you for three lines."
line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")

#向文件内写入上面输入的三个句子
print "I'm going to write these to the file."
#源代码(在6行内输出)
# target.write(line1)
# target.write("\n")
# target.write(line2)
# target.write("\n")
# target.write(line3)
# target.write("\n")

#在1行中输出
target.write("%s\n%s\n%s\n" % (line1, line2, line3)) 

print "And finally, we close it."
target.close()

"""
加分题
1如果你觉得自己没有弄懂的话,用我们的老办法,在每一行之前加上注解,为自己理清思路。就算不能理清思路,你也可以知道自己究竟具体哪里没弄明白。
2写一个和上一个练习类似的脚本,使用 read 和 argv 读取你刚才新建的文件。
	from sys import argv
	script, filename = argv
	txt = open(filename)
	print txt.read()

3文件中重复的地方太多了。试着用一个 target.write() 将 line1, line2, line3 打印出来,你可以使用字符串、格式化字符、以及转义字符。
	target.write("%s\n%s\n%s\n" % (line1, line2, line3)) 

4找出为什么我们需要给 open 多赋予一个 'w' 参数。提示: open 对于文件的写入操作态度是安全第一,所以你只有特别指定以后,它才会进行写入操作。
	因为默认open只能读取不能写入,所以要写入必须赋予'w'参数。
"""



L17更多文件操作

1.通过import导入更多功能,复用现成功能,而不是浪费时间再造轮子也是写程序很重要的一个思想。

2.首次用到exists功能。可以判断文件是否存在,返回一个布尔值。



#coding:utf-8
#习题 17: 更多文件操作

from sys import argv
from os.path import exists

script, from_file, to_file = argv

print "Copying from %s to %s" % (from_file, to_file)

input = open(from_file)
indata = input.read()

print "The input file is %d bytes long" % len(indata)

print "Does the output file exist? %r" % exists(to_file)
print "Ready, hit RETURN to continue, CTRL-C to abort."
raw_input()

output = open(to_file, 'w')
output.write(indata)

print "Alright, all done."
output.close()
input.close

"""
加分题:

1.再多读读和 import 相关的材料,将 python 运行起来,试试这一条命令。试着看看自己能不能摸出点门道,当然了,即使弄不明白也没关系。
	*模块和包的参考文档:http://wiki.woodpecker.org.cn/moin/PythonEssentialRef8
	import当然是用来引入各种模块、包,从而使用其中包含的方法,定义的数据等。具体等日后丰富。

2.这个脚本 实在是 有点烦人。没必要在拷贝之前问一遍把,没必要在屏幕上输出那么多东西。试着删掉脚本的一些功能,让它使用起来更加友好。
	见最后自己写的东西。

3.看看你能把这个脚本改多短,我可以把它写成一行。
	一行= =,input.close就一行了吧。。。试试看
	不算import行,参数定义行和关闭文件行,的确一行就够了的说:
	***********
	from sys import argv
	script, from_file, to_file = argv
        open(to_file, 'w').write(open(from_file).read())
	***********

4.我使用了一个叫 cat 的东西,这个古老的命令的用处是将两个文件“连接(con*cat*enate)”到一起,不过实际上它最大的用途是打印文件内容到屏幕上。你可以通过 man cat 命令了解到更多信息。
5.使用 Windows 的同学,你们可以给自己找一个 cat 的替代品。关于 man 的东西就别想太多了,Windows 下没这个命令。
	对Win满满的恶意=。=~~powershell下可以使用get-content [-Path]来读取txt文件

6.找出为什么你需要在代码中写 output.close() 。
	为了回收资源。

"""

"""
自己写的能够实现从txt1复制到txt2功能的代码:
from sys import argv
script, from_file, to_file = argv

txt = open(from_file)
target = open(to_file, 'w')
target.write(txt.read())

print "over"
txt.close
target.close
print "files are closed"
"""




评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值