新手学python-笨方法学python-ex16-读写文件

跟着笨方法学python个教程学习,边学边做习题边记录。

新手学python--ex16-读写文件件 - 学习记录

课程内容:

了解各种文件相关的命令(方法/函数):

close - 关闭文件。跟你编辑器的 文件->保存.. 一个意思。
read - 读取文件内容。你可以把结果赋给一个变量。
readline - 读取文本文件中的一行。
truncate - 清空文件,请小心使用该命令。
write(stuff) - 将stuff写入文件。

这是你现在该知道的重要命令。有些命令需要接受参数,这对我们并不重要。你只要记住write的用法就可以了。write需要接收一个字符串作为参数,从而将该字符串写入文件。

让我们来使用这些命令做一个简单的文本编辑器吧:

from sys import argv

script, filename = argv

print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-C(^C)."
print "If you do want that, hit RETURN."

raw_input("?")

print "Opening the file..."
target = open(filename, 'w')

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."

target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

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


这个文件是够大的,大概是你键入过的最大的文件。所以慢慢来,仔细检查,让它能运行起来。
有一个小技巧可以让你的脚本一部分一部分地运行起来。先写1-8行,让它运行起来;再多运行5行,再接着多运行几行,一次类推,直到整个脚本运行起来为止。

运行该脚本:

$python ex16.py ex16-sample.txt
We're going to erase 'ex16-sample.txt'.
If you don't want that, hit CTRL-C(^C).
If you do want that, hit RETURN.
?
Opening the file...
Truncating the file. Goodbye!
Now I'm going to ask you for three lines.
line 1: the 1st line for test.
line 2: the 2nd line.
line 3: the last line for check...
I'm going to write these to the file.
And finally,we close it.

$

接下来打开你新建的文件(我的是ex16-sample.txt),看看里面的内容。

the 1st line for test.
the 2nd line.
the last line for check...

 

加分习题:
1.如果你觉得自己没有弄懂的话,用我们的老办法,在每一行之前加上注解,为自己理清思路。就算不能理清思路,你也可以知道自己究竟具体哪里没弄明白。

2.写一个和上一个练习类似的脚本,使用 read 和 argv 读取你刚才新建的文件。

from sys import argv

script, filename = argv

print "We are going to erase %r." % filename
print "If you do not want that, hit CTRL-C(^C)."
print "If you do want that, hit RETURN."

raw_input("?")

print "Opening the file..."
target = open(filename, 'w')

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."

target.write(line1) 
target.write("\n")
target.write(line2) 
target.write("\n")
target.write(line3) 
target.write("\n")

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

print "To see the new file content."
txt = open(filename)
print txt.read()
txt.close()


运行该脚本:

$python ex16-2.py ex16-sample.txt
We are going to erase 'ex16-sample.txt'.
If you do not want that, hit CTRL-C(^C).
If you do want that, hit RETURN.
?
Opening the file...
Truncating the file. GoodBye...
Now I'm going to ask you for three lines.
line 1: abc
line 2: 123
line 3: hi hello
I'm going to write these to the file.
And finally,we close it.
To see the new file content.
abc
123
hi hello


$


3.文件中重复的地方太多了。试着用一个 target.write() 将 line1, line2, line3 打印出来,你可以使用字符串、格式化字符、以及转义字符。
3.1 将6行target.write()换成target.write("%s \n %s \n %s \n" ) % (line1, line2, line3),运行程序,出错
TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'
3.2上网查了一下,字符串连接用 + :
将6行target.write()换成 target.write(line1 + "\n" + line2 + "\n" + line3 + "\n") ,成功:


 

from sys import argv

script, filename = argv

print "We're going to erase %r." % filename

print """
If you do not want that, hit CTRL-C(^C).
If you do want that, hit RETURN.
"""

print "Opening the file ..."
target = open(filename, 'w')

print "Truncating the file ..."
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."

#target.write(line1 "\n" line2 "\n" line3 "\n")
target.write(line1 + "\n" + line2 + "\n" + line3 + "\n") 

print "And finally, we close it."

target.close()

print "Print the new contents of the file..."

newtxt = open(filename)
print newtxt.read()
newtxt.close

 

运行该程序:

$python ex16-3.py ex16-sample.txt
We're going to erase 'ex16-sample.txt'.

If you do not want that, hit CTRL-C(^C).
If you do want that, hit RETURN.

Opening the file ...
Truncating the file ...
Now I'm going to ask you for three lines.
line 1: test1
line 2: test2
line 3: test3
I'm going to write these to the file.
And finally, we close it.
Print the new contents of the file...
test1
test2
test3


$


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

至此,笨方法学-ex16-读写文件这一小节的学习结束了。

Monday, November 02, 2015 | Python

文章评论

No comments posted yet.

发表评论

Please add 2 and 3 and type the answer here: