文件操作流程
文件操作流程
1.打开文件,得到文件句柄并赋值给一个变量
2.通过句柄对文件进行操作
3.关闭文件
最基本的打开,只能阅读,不能操作.
1
2
3
|
data
=
open
(
"file"
,encoding
=
"utf-8"
).read()
print
(data)
print
(
type
(data))
#字符串格
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
f
=
open
(
"file"
,
"r"
,encoding
=
"utf-8"
) 文件句柄赋值给f
data
=
f.read()
data2
=
f.read() 当这里读取时,python将从上次data读取的最后开始读取,也就是什么也没有。
r 读取文件 不能写;
w 创建一个文件 不能读;
a 追加内容 不覆盖文件,不能读取。
f.read() 阅读
f.write() 写文件
注意:
U表示在读取时,可以将\r\n自动替换成\n(与r或r
+
模式同使用)
rU
r
+
U
b表示处理二进制文件(如:FTP发送上传文件,linux可忽略,windows处理二进制文件时需要标注)
|
读取前五行
1
2
3
4
5
6
7
8
|
方法一、
f
=
open
(
"file"
,
"r"
,encoding
=
"utf-8"
)
print
(f.readline())
print
(f.readline())
print
(f.readline())
print
(f.readline())
print
(f.readline())
f.close()
|
1
2
3
4
5
|
方法二、
f
=
open
(
"file"
,
"r"
,encoding
=
"utf-8"
)
for
i
in
range
(
5
):
print
(f.readline())
f.close()
|
第五行不打印
1
2
3
4
|
f.readlines() 是一个列表,
f
=
open
(
"file"
,
"r"
,encoding
=
"utf-8"
)
for
i
in
f.readlines():
print
(i.strip()) 每行后面默认有一个换行符,用strip去掉;
|
1
2
3
4
5
6
7
|
方法一、
f
=
open
(
"file"
,
"r"
,encoding
=
"utf-8"
)
for
index,i
in
enumerate
(f.readlines()):
if
index
=
=
4
:
print
(
"------分割线--------"
)
continue
print
(i.strip())
|
1
2
3
4
5
6
7
8
9
10
|
方法二、
f
=
open
(
"file"
,
"r"
,encoding
=
"utf-8"
)
count
=
0
for
i
in
f: f已经成为一个迭代器
if
count
=
=
4
:
print
(
"------分割线-------"
)
count
+
=
1
continue
结束本次循环,进行下一次循环
print
(i.strip())
count
+
=
1
|
文件方法:
f = open("file","r",encoding="utf-8")
查看当前指针位置
1
2
3
|
print
(f.tell())
print
(f.readline())
print
(f.tell())
|
指针回到文件开头
1
2
|
f.seek(
0
)
print
(f.tell())
|
查看文件编码
1
|
print
(f.encoding)
|
做异常处理 f.errors
查看是不是一个终端设备
1
|
print
(f.isatty())
|
判断光标是否可以移动,可以True 否则False
1
|
print
(f.seekable())
|
判断文件是否可读
1
|
print
(f.readable())
|
判断文件是否可写
1
|
print
(f.writable())
|
强制刷新;用于实时将写的数据写到硬盘上;
1
|
print
(f.flush())
|
flush 事例进度条
1
2
3
4
5
6
7
|
import
sys,time
for
i
in
range
(
100
):
sys.stdout.write(
" "
)
for
i
in
range
(
100
):
sys.stdout.write(
" "
)
sys.stdout.flush()
time.sleep(
0.1
)
|
判断文件是否关闭
1
|
print
(f.closed)
|
截断
1
2
3
|
f
=
open
(
"file"
,
"a"
,encoding
=
"utf-8"
)
f.truncate() 不加参数清空文件
f.truncate(
10
) 从文件开始截断
10
个字节
|
文件读写模式打开(读和追加写)
1
2
3
4
5
|
f
=
open
(
"file"
,
"r+"
,encoding
=
"utf-8"
)
print
(f.readline())
f.write(
"==============="
)
print
(f.readline())
f.close()
|
文件写读模式打开(创建一个新文件写和读,然后在追加写。)
1
2
3
4
5
6
7
8
9
10
|
f
=
open
(
"file"
,
"w+"
,encoding
=
"utf-8"
)
print
(f.readline())
f.write(
"===============\n"
)
f.write(
"===============\n"
)
print
(f.seek(
10
))
print
(f.readline())
f.write(
"+++++++++++++++\n"
)
print
(f.seek(
10
))
print
(f.readline())
f.close()
|
文件追加读写模式打开(可以追加写和读)
1
2
3
4
5
6
7
8
9
10
11
|
f
=
open
(
"file"
,
"a+"
,encoding
=
"utf-8"
)
print
(f.tell())
print
(f.readline())
f.write(
"===============\n"
)
f.write(
"===============\n"
)
print
(f.seek(
10
))
print
(f.readline())
f.write(
"+++++++++++++++\n"
)
print
(f.seek(
10
))
print
(f.readline())
f.close()
|
文件二进制编码方式读;应用于网络传输
1
2
|
f
=
open
(
"file"
,
"rb"
)
print
(f.readline())
|
文件二进制编码方式写;应用于网络传输
1
2
3
|
f
=
open
(
"file"
,
"wb"
)
print
(f.write(
"hhhhhhh"
.encode())) 默认是utf
-
8
模式
f.close()
|
文件二进制编码方式追加;应用于网络传输
1
2
3
|
f
=
open
(
"file"
,
"ab"
)
print
(f.write(
"hhhhhhh"
.encode())) 默认是utf
-
8
模式
f.close()
|
文件修改
1.全部加载到内存里,重新写到文件里
2.修改后,写在另一个文件里
1
2
3
4
5
6
7
8
9
|
f
=
open
(
"file"
,
"r"
,encoding
=
"utf-8"
)
f2
=
open
(
"file2"
,
"w"
,encoding
=
"utf-8"
)
for
i
in
f:
if
"2222"
in
i:
i
=
i.replace(
"2222"
,
"BBBB"
)
#i.replace替换功能
f2.write(i)
f.close()
f2.close()
|
文件操作自动关闭with
特点:自动关闭文件。
为了避免打开文件后忘记关闭,可以通过管理上下文。
1
2
|
例如:with
open
(
"file"
,
"r"
)as f:
print
(f.readline())
|
python2.7以后可以同时打开多个文件。
1
2
3
4
5
6
|
with
open
(
"file"
,
"r"
) as f,\
open
(
"file2"
,
"r"
) as f2:
for
i
in
f:
print
(i.strip())
for
i2
in
f2:
print
(i2.strip())
|
(python官方要求:一行代码不要超过80个代码;)
本文转自 506554897 51CTO博客,原文链接:http://blog.51cto.com/506554897/1945833