python--文件操作

来源:互联网 发布:单机游戏翻译软件 编辑:程序博客网 时间:2024/06/10 19:48

1.文件打开与关闭

1.1打开文件

a. 使用open(filename)函数打开文件

from sys import argvscript, filename = argvtxt = open(filename)#代开后使用read函数读取文件内容输出print txt.read()

b.再次运行 python 在命令行下使用 open 打开一个文件,这种 open 和 read 的方法也值得一学。

   >python   >print open("15.txt").read()

两种方式输出都相同,即为文件中的内容

This is stuff I typed into a file.It is really cool stuff.Lots and lots of fun to have in here.

Q:为什么打开了两次文件没有报错?
A:Python 不会限制你打开文件的次数,事实上有时候多次打开同一个文件是一件必须的事情。

1.2文件关闭

处理完文件后你需要将其关闭,这是很重要的一点,关闭文件close函数

text.close()

2.读写文件

2.1函数及例子

• close – 关闭文件。跟你编辑器的 文件->保存.. 一个意思。
• read – 读取文件内容。你可以把结果赋给一个变量。
• readline – 读取文本文件中的一行。
• truncate – 清空文件,请小心使用该命令。
• write(stuff) – 将stuff 写入文件。write 需要接收一个字符串作为参数,从而将该字符串写入文件
• exists(filename)-将文件名字符串作为参数,如果文件存在的话,它将返回 True,否则将返回 False。使用时要使用import调用:from os.path import exists
• len(data) - 返回字符串的长度

from sys import argvscript, filename = argvprint "We're going to erase %r." % filenameprint "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()

2.2文件打开读写的模式

python的open()方法用来打开一个文件。第一个参数时文件的位置和文件名,第二个为可选参数,指定打开读写的模式。
f=open(‘1.txt’,’w’)
读写模式的类型有:
w 以写的方式打开,文件若存在,首先要清空,然后(重新创建)
a 以追加模式打开(从EOF开始,必要时创建新文件),把所有要写入文件的数据追加到文件的末尾,即使使用seek()指向了文件的其它地方,如果文件不存在,将自动创建。
r+ 以读写模式打开
W+ 以读写模式打开
a+ 以读写模式打开
rb 以二进制读写模式打开
wb 以二进制写模式打开
ab 以二进制追加模式打开
rb+ 以二进制读写模式打开
wb+ 以二进制读写模式打开
ab+ 以二进制读写模式打开
如果只写 open(filename) 那就使用 ‘r’ 模式打开,这是 open() 函数的默认工作方式.

2.3文件的关闭与检测

使用open()打开文件后一定要记得关闭文件对象,f.close()
可以用try()finally()确定关闭文件。
file=open()
try:
atxt=file.read()
finally:
file.close()
注:不能把open语句放到try块里面,因为打开文件出现异常时,文件对象无法close()

2.4读文件

read([size])
readline([size])
readlines([size])//读取缓存块大小的文件内容 以列表的形式返回

2.5写文件

写入文件时,执行write()后内容不会马上写入文件,要用f.close()或者f.flush()命令,才会写入。
或者当写入的内容的大小大于缓存块的大小时,系统会自动将缓存块中的内容刷入文件,然后再继续相缓存中写入内容。(所以当文件足够大时系统也不一定会自动把所有内容都写入文件)

2.6文件的指针

当新建的文件时,指针指在文件的开始处,从0开始,当写入或读取内容时,文件的指针从0位置向后移动,当再次读取时,从指针的位置开始读取。因此当向文件写入内容后,要关闭文件再读取才会看到文件中有内容。
要让文件中的指针回到某个位置可以用命令f.seek(偏移量,参数)参数包括os.SEEK.CUR os.SEEK.END os.SEEK.SET
seek() 函数的处理对象是 字节 而非行,所以 seek(0) 只是转到文件的 0 byte,也就是第一个 byte 的位置。
查看文件的指针所在位置的命令f.tell()

3.更多文件操作

编写一个 Python 脚本,将一个文件中的内容拷贝到另外一个
文件中

from sys import argvfrom os.path import existsscript, from_file, to_file = argvprint "Copying from %s to %s" % (from_file, to_file)# we could do these two on one line too, how?in_file = open(from_file)indata = in_file.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()out_file = open(to_file, 'w')out_file.write(indata)print "Alright, all done."out_file.close()in_file.close()

Q:当关闭文件时出问题
A:
可能是写了indata = open(from_file).read() 这意味着你无需再运行
in_file.close() 了,因为 read() 一旦运行, 文件就会被读到结尾并且被 close 掉.

0 0
原创粉丝点击