说明:
主要是file()和open()函数的使用,但在查open()函数的帮助时,会有下面的说明:
1
2
3
|
>>> help(open)
……
Open a file using the file() type, returns a file object.
|
因此,两个函数其实都是一样的,下面只用file()。
在列举file()的作用时,使用help即是很好的方法,下面则是应重点关注的内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
close(...)
| close() -> None or (perhaps) an integer. Close the file.
flush(...)
| flush() -> None. Flush the
internal
I/O buffer.
readline(...)
| readline([size]) -> next line from the file,
as
a string.
readlines(...)
| readlines([size]) -> list of strings,
each
a line from the file.
seek(...)
| seek(offset[, whence]) -> None. Move to
new
file position.
tell(...)
| tell() -> current file position, an integer (may be a long integer).
write(...)
| write(str) -> None. Write string str to file.
writelines(...)
| writelines(sequence_of_strings) -> None. Write the strings to the file.
xreadlines(...)
| xreadlines() -> returns self.
|
1.创建文件
--基本格式:
1
2
3
|
f = file(
'test.txt'
,
'w'
)
f = write(
'Hello World!'
)
f.close()
|
·w:写模式,文件不存在就创建,存在则自动覆盖原来的内容,只能写,不能读;
·w+:写读模式,但一开始还是会清空原来文件内容,只是在写文件之后可以读取;
·写的内容放在内存当中,如果要写入磁盘,可以f.close()关闭文件或f.flush()实时写入磁盘;
·不可以实时改变模式,只能把文件关闭后,再次打开时定义模式;
--实例:
1
2
3
4
5
|
>>> f = file(
'test.txt'
,
'w'
)
>>> f.write(
'Hello World!'
)
>>> f.flush()
xpleaf@xpleaf-machine:~/seminar6/day2$ more test.txt
Hello World!
|
--write()与writelines()
·前者写入的内容只能是字符串,后者则可以写入列表:
1
2
3
4
5
6
|
>>> f.write([
'a'
,
'b'
,
'c'
])
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
TypeError: expected a character buffer object
>>> f.writelines([
'a'
,
'b'
,
'c'
])
>>>
|
--f.close()的重要说明
·如果没有f.close(),则在程序运行结束后,系统会自动帮我们关闭文件;
·长时间运行的程序,需要打开并编辑文件(如用'a'模式),没有关闭文件,会导致文件内容无法保持一致性的问题(如果系统中有其他程序需要编辑该文件);
·Linux中的Vim编辑器自带文件锁定功能,即不能同时编辑同一文件;
·Python中文件的锁是没有加上的,需要开发者自行为文件加锁。
2.读取文件与遍历文件内容
--基本格式:
1
2
3
|
f = file(
'test.txt'
,
'r'
) ===>可以不加
'r'
,默认就是该模式
f = read()
f.close()
|
·r:默认;
·r+:读写模式,可以尝试使用,每读取一行,指针就跳到下一行,写的时候,就直接覆盖掉指针指的这一行;
·rb:在windows平台下编辑的文件,在linux中用python进行读取时,模式要选择“rb”,否则有可能会出现乱码的现象,即跨平台的文件都要注意此点;
--read()、readline、readlines()与xreadlines()
·前三者都是直接把文件内容全部写入内存当中,然后再全部读取或一行一行地读取;
·都采用迭代的方式读取,即指针最开始指向第一行,读取第一行后,指针指向下一行;
-read()
·把文件内容全部读取:
1
2
3
4
5
|
>>> f = file(
'test.txt'
,
'r'
)
>>> f.read()
"Hello World!\nI'm xpleaf.\nNice to meet you!\n"
>>> f.read()
''
===>内容已经读完,即指针已经在最后一行,后面没有内容
|
·可以用tell()查看当前指针的位置:
1
2
|
>>> f.tell()
43
===>
43
,即是最后一个字符
|
·重新读取文件内容,可以f.close()后再次打开,也可以使用f.seek(0):
1
2
3
4
5
6
7
|
>>> f.seek(
0
) ===>重新寻址,让指针指向文件最开始
>>> f.tell()
0
>>> print f.read()
Hello World!
I'm xpleaf.
Nice to meet you!
|
-readline()
·以字符串方式,一行一行地读取文件内容:
1
2
3
4
5
6
7
8
9
|
>>> f.seek(
0
)
>>> f.readline()
'Hello World!\n'
>>> f.readline()
"I'm xpleaf.\n"
>>> f.readline()
'Nice to meet you!\n'
>>> f.readline()
''
|
-readlines()
·以列表的方式,一行一行地读取文件内容,一行即为列表中的一个元素:
1
2
3
4
5
|
>>> f.seek(
0
)
>>> f.readlines()
[
'Hello World!\n'
,
"I'm xpleaf.\n"
, 'Nice to meet you!\n']
>>> f.readlines()
[]
|
·因此,习惯性的用法是:修改文件内容
1
2
3
4
5
6
7
|
>>> f.seek(
0
)
>>> filelist = f.readlines()
>>> print filelist
[
'Hello World!\n'
,
"I'm xpleaf.\n"
, 'Nice to meet you!\n']
>>> filelist[
2
] =
'See you next time!'
>>> print filelist
[
'Hello World!\n'
,
"I'm xpleaf.\n"
, 'See you next time!']
|
·再以w的方式打开文件,用f.writelines(filelist)的方式写入,即可实现修改文件内容的目的;
-xreadlines()
·不是先把文件内容全部写入内存,而是每读取一行才写入一行,写下一行时即对前面内存中的内容进行回收;
·在读取较大文件时,适宜采用这种办法。
--文件内容的遍历:使用readlines()
1
2
3
4
5
6
7
8
|
>>> f = file(
'test.txt'
,
'r'
)
>>> filelist = f.readlines()
>>>
for
eachline
in
filelist:
... print eachline,
...
Hello World!
I'm xpleaf.
Nice to meet you!
|
3.文件内容追加
--基本格式:
1
2
3
|
f = file(
'test.txt'
,
'a'
)
f = write(
'Hello World!'
)
f.close()
|
·文件内容追加到最后一行上,如果最后一行有'\n',则追加到下一行;
·write只能添加字符串,如果是数值或其它类型的数据类型,则需要使用str()进行转换;
--实例:
1
2
3
4
5
6
7
8
9
|
>>> f = file(
'test.txt'
,
'a'
)
>>> f.write(
'See you next time!'
)
>>> f.write(
'I will miss you much!\n'
)
>>> f.flush()
xpleaf@xpleaf-machine:~/seminar6/day2$ cat test.txt
Hello World!
I'm xpleaf.
Nice to meet you!
See you next time!I will miss you much!
|
4.文件内容替换
--基本格式:
1
2
3
4
|
import
fileinput
for
line
in
fileinput.input(
'filepath'
, inplace =
1
):
line = line.replace(
'oldtext'
,
'newtext'
)
print line,
|
·inplace = 1,表示要修改文件内的内容,默认值为0,表示不修改文件内容,加“print line,”时只打印内存中修改的内容(看下面例子);
·inplace = 1时,如果不加“print line,”,原来文件内容会为空;
·可以额外加backup参数,表示在修改文件内容时进行备份;
--实例:
-正确操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>>
import
fileinput
>>>
for
line
in
fileinput.input(
'test.txt'
, inplace =
1
, backup =
'.ori'
):
... line = line.replace(
'Hello World!'
,
'Hello, everyone!'
)
... print line,
...
xpleaf@xpleaf-machine:~/seminar6/day2$ ls -l test*
-rw-rw-r--
1
xpleaf xpleaf
87
9
月
4
15
:
32
test.txt
-rw-rw-r--
1
xpleaf xpleaf
83
9
月
4
15
:
19
test.txt.ori
xpleaf@xpleaf-machine:~/seminar6/day2$ cat test.txt
Hello, everyone!
I'm xpleaf.
Nice to meet you!
See you next time!I will miss you much!
|
-如果没有加inplace = 1时:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>>
for
line
in
fileinput.input(
'test.txt'
):
... line = line.replace(
'Nice'
,
'Well'
)
... print line,
...
Hello, everyone!
I'm xpleaf.
Well to meet you!
See you next time!I will miss you much!
xpleaf@xpleaf-machine:~/seminar6/day2$ cat test.txt
Hello, everyone!
I'm xpleaf.
Nice to meet you!
See you next time!I will miss you much!
|
-如果没有加“print line,”时:
1
2
3
4
5
6
7
8
|
>>>
for
line
in
fileinput.input(
'test.txt'
):
... line = line.replace(
'Nice'
,
'Well'
)
...
>>>
for
line
in
fileinput.input(
'test.txt'
, inplace =
1
):
... line = line.replace(
'Hello'
,
'Hey'
)
...
xpleaf@xpleaf-machine:~/seminar6/day2$ cat test.txt
xpleaf@xpleaf-machine:~/seminar6/day2$ ===>文件内容已被清空
|