open+close: open && close 打开-关闭
with ,自动关闭
常见的操作
file = open("data.txt","w") # file是一个变量-->open 打开 data.txt这个文件 以写入的方式打开
file.write("My Name Is ZhangSan") # file.write 在data.txt 中写入My Name Is ZhangSan
file.close() # 最后关闭此文件
文件内容为: My Name Is ZhangSan
读:
with open("data.txt","r") as f: # with open 以读取的方式打开data.txt 文件 给一个别名叫做f
data_result = f.readline() # data_result 是一个变量 通过f 别名 使用readline() 方法 读取第一行 赋值给变量data_result
print(data_result) # 最后print 输出data_result的 内容
>>> My Name Is ZhangSan
with open("data.txt","a") as f: # with open 打开data.txt 这个文件以追加的方式 as f 别名为f
f.write("zhangsan1\n") # 第一行写入zhangsan1 \n 换行
f.write("zhangsan2\n") # 第二行写入zhangsan2 \n 换行 以此类推
f.write("zhangsan3\n")
f.write("zhangsan4\n")
f.write("zhangsan5\n")
验证追加结果:
with open("data.txt","r") as f: # with open 打开data.txt 文件以读取的方式打开 别名为f
print(f.readline()) # 一行一行的输出
print(f.readline())
print(f.readline())
print(f.readline())
print(f.readline())
>>>python
My Name Is ZhangSanzhangsan1
zhangsan2
zhangsan3
zhangsan4
zhangsan5
with open("data.txt","r") as f:
result = f.readline()
print(f"这是readline输出的结果---->{result}")
with open("data.txt","r") as f:
data_result = f.readlines()
print(f"这是readlines输出的结果---->{data_result}")
>>>这是readline输出的结果---->My Name Is ZhangSanzhangsan1
>>>这是readlines输出的结果---->['My Name Is ZhangSanzhangsan1\n', 'zhangsan2\n', 'zhangsan3\n', 'zhangsan4\n', 'zhangsan5\n']
file = open("data.txt")
print(file.tell())
print(file.readline())
print(file.tell())
print(file.readline())
print(file.tell())
print(file.readline())
file.seek(0)
print(file.tell())
print(file.readline())
file.seek(0)
print(f"这是第0个位置{file.readline()}")
>>>
0
My Name Is ZhangSanzhangsan1
30
zhangsan2
41
zhangsan3
0
My Name Is ZhangSanzhangsan1
这是第0个位置My Name Is ZhangSanzhangsan1
匹配后缀名文件
import os
import fnmatch
path_test = r"F:\python_test1\Yankerp_test\result_test"
for f in os.listdir(path_test):
if fnmatch.fnmatch(f, "*.txt"):
print(f)
>>>
0.txt
1.txt
2.txt
3.txt
4.txt
aaa.txt
yankerp.txt
例如2:
import os
import fnmatch
path_test = r"F:\python_test1\Yankerp_test\result_test"
for f in os.listdir(path_test):
if fnmatch.fnmatch(f, "*.doc"):
print(f)
>>>
0.doc
0l4Vx.doc
1.doc
2.doc
3.doc
4.doc
8A5ui.doc
bCy0P.doc
DLPK1.doc
KrHiC.doc
n7lv3.doc
yankai.doc
import os
import fnmatch
path_test = r"F:\python_test1\Yankerp_test\result_test"
for f in os.listdir(path_test):
if fnmatch.fnmatch(f, "?.doc"):
print(f)
>>>
0.doc
1.doc
2.doc
3.doc
4.doc
可以结合正则匹配:
import os
import fnmatch
path_test = r"F:\python_test1\Yankerp_test\result_test"
for f in os.listdir(path_test):
if fnmatch.fnmatch(f, "[1-9].pptx"):
print(f)
>>>
2.pptx
3.pptx
import os
import fnmatch
import glob
path_test = r"F:\python_test1\Yankerp_test\result_test"
for f in glob.glob(f"{path_test}/[0-9].txt"):
print(f)
>>>
F:\python_test1\Yankerp_test\result_test\0.txt
F:\python_test1\Yankerp_test\result_test\1.txt
F:\python_test1\Yankerp_test\result_test\2.txt
F:\python_test1\Yankerp_test\result_test\3.txt
F:\python_test1\Yankerp_test\result_test\4.txt
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pickle
User_data = {
"Name" : "Yankai",
"Password" : "Pwd123"
}
with open("data.pkl","wb") as f:
pickle.dump(User_data, f)
with open("data.pkl","rb") as f:
data = pickle.load(f)
print(data)
>>>
{'Name': 'Yankai', 'Password': 'Pwd123'}