一、文件操作
1)打开文件
path = 'test.txt'
path = r'/home/pyvip/py_case/test.txt'
file = open(path, 'w+')
2)写入
file.write('Python')
file.writelines([‘1’, ‘2’, ‘3’]
3)读取与关闭
file.read()
file.readline()
file.readlines()
file.close()
读取(read) 读一行(readline) 返回一个列表(readlines)
4)查看与移动文件指针
file.tell()
file.seek(0, 0)
二、文件操作流程
1)打开、读、关闭
f = open(r"C:\Users\JiaNeng\Desktop\demo.txt", 'r', encoding='utf-8') # 绝对地址
f = open("demo.txt", 'r', encoding='utf-8') # 相对地址
print(f.read())
f.close()
2)文件的基本操作:写入
f = open("demo.txt", "w", encoding="utf-8")
f.write("hello,world")
f.writelines(["hello,TZ\n", "2", "3"])
f.close()
3)write和writelines区别
1.write()传入的是字符串
2.writelines()传入既可以是字符串也可以是字符序列,注意不能是数字序列
"""
f = open("demo.txt", 'r', encoding='utf-8')
print(f.read())
4)刷新缓冲区,保存内容
f.flush()
关闭文件
f.close()
5)with语句
f = open("demo.txt", "w", encoding="utf-8")
f.writelines(["hello,world\n", "hello,TZ\n", "5"])
f.close()
f = open("demo.txt", "r", encoding="utf-8")
print(f.read())
f.close()
with open("demo.txt", "w", encoding="utf-8") as f:
f.writelines(["你好世界\n", "你好潭州\n", "5"])
with open("demo.txt", "r", encoding="utf-8") as f:
print(f.read())
6)光标
tell() 以bytes为单位,从文件头到当前指针所在位置
seek() 以bytes为单位,光标的偏移量
print("你好世界".encode("utf-8")) # b'\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xb8\x96\xe7\x95\x8c'
with open("demo.txt", "r", encoding="utf-8") as f:
print("1:", f.tell())
print(f.read())
print("2:", f.tell())
f.seek(0)
print("3:", f.tell())
f.seek(2)
print("4:", f.tell())
print(f.read())
7)r和w:只读和只写
r只读
with open("demo.txt", "r", encoding="utf-8") as f:
print(f.tell())
print(f.read())
w只写,文件已经存在会覆盖写入,不存在则创建文件写入
with open("demo.txt", "w", encoding="utf-8") as f:
f.write("hahaha")
with open("demo1.txt", "w", encoding="utf-8") as f:
f.write("hello")
8)rb和wb:只读和只写,非文本的读取和写入
s = ""
with open("联系方式.png", "rb")as f:
print(f.tell())
s = f.read()
with open("test.png", "wb")as f:
f.write(s)
print("="*50)
9).a追加:在文件末尾增加,文件不存在创建新的文件
with open("demo.txt", "r", encoding="utf-8") as f:
print(f.tell())
print(f.read())
with open("demo.txt", "a", encoding="utf-8") as f:
print(f.tell())
f.write("托马斯")
with open("demo.txt", "r", encoding="utf-8") as f:
print(f.tell())
print(f.read())
with open("demo2.txt", "a", encoding="utf-8") as f:
print(f.tell())
f.write("托马斯")
10)r+:读写,指针在文件开头
w+:读写,文件已经存在会覆盖写入,不存在则创建文件写入
a+:读写,文件存在则在文件末尾追加,文件不存在创建新的文件
with open("demo.txt", "r+", encoding="utf-8")as f:
print(f.tell())
print(f.read())
f.write("nihao")
with open("demo.txt", "w+", encoding="utf-8")as f:
f.read()
f.write("=====")
with open("demo.txt", "a+", encoding="utf-8")as f:
f.read()
f.write("=====")
二、上下文管理器
1)enter__和__exit__
通过这两个方法可以方便的实现上下文管理
with会把 enter 的返回值赋值给 as 后的变量
2)IO流
1、StringIO
创建IO操作(临时存储):
import io
sio = io.StringIO()
写入:
sio.write(str(i))
读取:
sio .getvalue()
StringIO在内存中如同打开文件一样操作字符串,因此也有文件的很多方法
当创建的StringIO调用 close() 方法时,在内存中的数据会被丢失
2、BytesIO
创建BytesIO:
import io
bio = io.BytesIO()
写入:
bio.write(b'abcd')
读取:
sio .getvalue()
BytesIO 和 StringIO 类似,但是BytesIO操作的是 Bytes数据
三、常用工具
1)os 操作系统交互
import os 模块提供python和操作系统交互的接口
直接调用系统命令
os.system('ls')
通用路径操作:
os.path
os.path.join(r'/home/pyvip', r'pycase')
文件目录操作:
os.mkdir('test')(新建文件夹)
os.rename('test', 'test1')(重命名)
….
os 提供了Python和操作系统交互方式,只要是和操作系统相关,就可以尝试在os模块中找方法
2)shutil 高级文件操作
mport hutil 模块提供了许多关于文件和文件集合的高级操作
移动文件
shutil.move
复制文件夹
shutil.copytree
删除文件夹
shutil.rmtree