example 15:
from sys import argv
script, filename = argv
txt = open(filename)
print "Here's your file %r:" % filename
print txt.read()
txt.close()
open目的是读取filename的目录,read才是读取内容
close() ,处理完文件后你需要将其关闭,这是很重要的一点。
close -- Closes the file. Like File->Save.
read -- Reads the contents of the file. You can assign the result to a variable.
readline -- Reads just one line of a text file.(靠找/n来查找第几行,会自动记住所到行数)
truncate -- Empties the file.
write('stuff') -- Appends "stuff" to the file.
seek(0) 找到0 byte位置
关于open(file(name[, mode[, buffering]])
The mode can be 'r', 'w' or 'a' for reading (default), writing or appending.
Add a 'b' to the mode for binary files.
Add a '+' to the mode to allow simultaneous reading and writing.
close -- Closes the file. Like File->Save.
read -- Reads the contents of the file. You can assign the result to a variable.
readline -- Reads just one line of a text file.(靠找/n来查找第几行,会自动记住所到行数)
truncate -- Empties the file.
write('stuff') -- Appends "stuff" to the file.
seek(0) 找到0 byte位置
关于open(file(name[, mode[, buffering]])
The mode can be 'r', 'w' or 'a' for reading (default), writing or appending.
Add a 'b' to the mode for binary files.
Add a '+' to the mode to allow simultaneous reading and writing.
没有评论:
发表评论