1
2
3
|
>>> help(open)
……
Open a file using the file() type, returns a file object.
|
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
2
3
|
f = file(
'test.txt'
,
'w'
)
f = write(
'Hello World!'
)
f.close()
|
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!
|
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'
])
>>>
|
1
2
3
|
f = file(
'test.txt'
,
'r'
) ===>可以不加
'r'
,默认就是该模式
f = read()
f.close()
|
1
2
3
4
5
|
>>> f = file(
'test.txt'
,
'r'
)
>>> f.read()
"Hello World!\nI'm xpleaf.\nNice to meet you!\n"
>>> f.read()
''
===>内容已经读完,即指针已经在最后一行,后面没有内容
|
1
2
|
>>> f.tell()
43
===>
43
,即是最后一个字符
|
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!
|
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()
''
|
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!']
|
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!
|
1
2
3
|
f = file(
'test.txt'
,
'a'
)
f = write(
'Hello World!'
)
f.close()
|
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!
|
1
2
3
4
|
import
fileinput
for
line
in
fileinput.input(
'filepath'
, inplace =
1
):
line = line.replace(
'oldtext'
,
'newtext'
)
print line,
|
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!
|
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!
|
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$ ===>文件内容已被清空
|