python文件操作

最基本的文件操作当然就是在文件中读写数据。这也是很容易掌握的。现在打开一个文件以进行写操作: 

1. fileHandle = open ( 'test.txt', 'w' )  (文件句柄是个对象,与c不同

fileHandle = open ( 'test.txt', 'w' ) 注意,我们是以w打开,使用f.read()会报错.

‘w'是指文件将被写入数据,语句的其它部分很好理解。下一步就是将数据写入文件: 

1. fileHandle.write ( 'This is a test.\nReally, it is.' )  

fileHandle.write ( 'This is a test.\nReally, it is.' ) 

这个语句将“This is a test.”写入文件的第一行,“Really, it is.”写入文件的第二行。最后,我们需要做清理工作,并且关闭文件: 

1. fileHandle.close()  

正如你所见,在Python的面向对象机制下,这确实非常简单。需要注意的是,当你再次使用“w”方式在文件中写数据,所有原来的内容都会被删除。如果想保留原来的内容,可以使用“a”方式在文件中结尾附加数据。

读取test.txt,并将内容显示出来: 

1. fileHandle = open ( 'test.txt' )  
2. print fileHandle.read()  
3. fileHandle.close() 

  1. f.read(size)  

参数size表示读取的数量,可以省略。如果省略size参数,则表示读取文件所有内容。

open mode

"a" Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist. 
"r+" Open a file for update both reading and writing. The file must exist. 
"w+" Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file
"a+" Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.

当文件不存在时,r+不会创建,而会导致调用失败,但w+会创建。

如果文件存在,r+不会自动清空文件,但w+会自动把已有文件的内容清空。

另外,r+打开文件后,会把读写指针指向文件的开头,所以会覆盖已有的文件

简要提炼下:

r+ 和 w+ 的区别:

 r+ 是可以直接写在文件上,读取和写入的光标都在文件开头。

 w+ ,如果文件已经存在,将建立一个新文件覆盖原文件(很缺德啊……),并且支持读取。

a+ 和 r+:

 a+只能在文件最后补充,光标在结尾。

 r+可以覆盖前面的内容,光标在开头

f.readlines()   返回一个list

读取所有的行到数组里面[line1,line2,...lineN]。在避免将所有文件内容加载到内存中,这种方法常常使用,便于提高效率。

我们做如下实验:

在C盘创建data.txt 文件内容:

first line
second line
third line

然后运行如下程序:

f=open("C:/data.txt","w+")

f.read()

发现输出为 ' '(因为w+会把已存在的文件内容清空)。

我们改为

f=open("C:/data.txt","r+")

f.read()

输出:

'first line\nsecond line\nthird line\n'

接着:

f.readlines()
[]

为什么?因为此时文件指针已经到尾了。

使用f.seek(0)定位指针移动到文件开始。

>>>f.seek(0)

>>>f.readlines()
['first line\n', 'second line\n', 'third line\n']

f.read()和f.readlines()区别

When you create a file object in Python you can read from it in several different ways. Not until today did I understand the difference between readline() and readlines(). The answer is in the name. readline() reads one line character at a time, readlines()reads in the whole file at once and splits it by line.

These would then be equivalent:

f = open('somefile.txt','r')
for line in f.readlines():
    print line
f.close()

# ...and...

f = open('somefile.txt','r')
for line in f.read().split('\n'):
    print line
f.close()

The xreadlines() function should be used for big files:

f = open('HUGE.log','r'):
for line in f.xreadlines():
   print line
f.close()

From Charming Python

" The difference between .readline() and .readlines() is that the latter, like .read(), reads in an entire file at once. .readlines() automatically parses the read contents into a list of lines, thereby enabling the for ... in ... construct common in Python. Using .readline() reads in just a single line from a file at a time, and is generally much slower than .readlines(). Really the only reason to use the .readline() version is if you expect to read very large files that might exceed available memory."

UPDATE

Thanks commentors for pointing out that I and the Charmed Python book got it completely wrong. readline() reads one character at a time. xreadlines()reads one line at a time. I've changed the example code above.

 打断无限循环

Python并不像C语言有”do/while”语句,它只有一个while循环语句和for循环语句,有时你并不会提前知道什么时候循环会结束,或者你需要打破循环。一个不能再普通的例子就是正在按行迭代一个文件内容:

file = open("some_filename", "r")

while 1:   # infinite loop
    line = file.readline()
    if not line:  # 'readline()' returns None at end of file.
        break

    # Process the line.

这样做并不聪明,但是足够规范。对于文件来说还有一个更漂亮的做法:

file = open("some_filename", "r")

for line in file:(这里file作为一个iterable)
    # Process the line.

注意Python中也有一个continue语句(就像C中的)来跳过本次循环进入下一次循环。

注意内置函数 file() 跟 open() 一样,而且现在更流行了(因为对象的构造器应该和这个对象一个名字)

参考:http://www.jb51.net/article/15709.htm

 

原文地址:https://www.cnblogs.com/youxin/p/3061519.html