Python文件处理操作
一、文件的三种基本操作
r模式:
r,r+,rb,rb+
- r:只读不写,不可新建
- r+:可读可写,不可新建,可多次写有可能会覆盖原始内容
- rb:用于打开二进制文件,只读,一般用于非文本文件,像图片等,二进制文件把内容表示为一个特殊的bytes字符串类型
- rb:以二进制格式打开一个文件用于读写,文件的指针将会放在文件的开头,一般用于非文本文件和图片等
代码演示:
f=open("myfile.txt",'r',encoding='utf-8')
print(f.read()) # 输出myfile.txt里面的内容
f=open("myfile.txt",'r+',encoding='utf-8')
f.write("hello world")
f.write("Hello world!")
print(f.read()) #输出为空,因为执行写入操作后文件指针将会放在文件的末尾,此时需要把文件指针移动到下标为0的位置
f.seek(0)
print(f.read() #此时输出写入的内容
f=open(r"picture.jpg",'rb')
print(f.read(100)) # 读取的是文件的字节数,100表示限制读取的字节数
f=open(r"pictrue.jpg",rb+)
# 以读和写的方式打开图片
w模式
w,w+,wb,wb+
w:打开一个文件,只用于写入,若文件不存在则创建新文件,会覆盖原有内容 w+:打开一个文件,用于读写,若文件不存在则创建新文件,会覆盖原有内容 wb:打开一个二进制文件只用于写入 wb+:打开一个二进制文件用于读写操作 代码演示:
f=open("myfile1.txt","w",encoding="utf-8")
f.write("123")
f=open("myfile1.txt","w+",encoding="utf-8")
f.write("123")
f.seek(0)
print(f.read()) #输出123
f=open("myfile1.txt","wb")
f=open("myfile1.txt","wb+")
a模式
a,a+,ab,ab+
- a:打开文件用于追加,文件存在,文件指针放在文件的结尾,若文件不存在创建新文件继续写入
- a+:读写追加
- ab:追加二进制文件
- ab+:读写追加二进制文件
代码演示:
f=open("myfile1.txt","a",encoding="utf-8")
f.write("123")
f=open("myfile1.txt","a",encoding="utf-8")
f.write("追加内容")
s.seek(0)
print(f.read()) #会将原内容和追加内容一起显示出来
f=open("myfile1.txt","ab")
f=open("myfile1.txt","ab+")
二、os模块的一些文件操作:
代码演示:
import os
# 给文件重命名操作
os.rename("照片.pbg","图片.png")
# 删除指定文件,这方法会有一个参数,这个参数就是要删除的文件的名称,若有相对路径,则要配合相对路径使用文件名称。
os.remove("img1/p1.png")
# 获取当前程序文件的工作目录
os.getcwd()
# 判断是否为文件或目录
print(os.path.isfile("files/a.txt")) # 若为文件则返回True,否则返回False
print(os.path.isdir("files")) # 若为目录则返回True,否则返回False
#判断文件是否存在
if os.path.exists("files/stroy.txt"):
print("文件存在")
else:
print("文件不存在")
# 创建目录
os.mkdir("dir")
# 删除目录,删除单级目录,不能进行嵌套目录的删除
os.rmdir("dir")
# 创建多级目录
os.makedirs("mystory/myfile/file")
#删除多级空目录,如果myfile为空则删除,如果mystory也为空则删除,依此类推,如果上一层为非空则停止删除
os.removedirs("mystory/myfile")
三、文件写入操作的一些事项
1.文件写入
# 写入一行文件功能的代码:
with open('myfile.txt','w+',encoding='utf-8') as fs:
fs.write("你好啊,读书人!")
fs.seek(0)
print(fs.read)
# 写入多行文件功能的代码:
with open('myfile.txt','w+',encoding='utf-8') as fs:
fs.write("你好啊,读书人!\n") #写入多行是需要加入换行符的
fs.write("hello world\n")
fs.seek(0)
print(fs.read)
2.文件读取
# 单行读取
with open('myfile.txt','r',encoding='utf-8') as fs:
print(fs.readline()) #只读取第一行
#多行读取
# 1、使用readlines()
with open('myfile.txt','r',encoding='utf-8') as fs:
print(fs.readlines()) #读取全部行,平且以列表形式返回并且带有换行符
data=[line.strip for line in fs.readlines()] #去除换行符
print(data) #strip()方法默认会移除字符串首尾的空白字符,包括换行符、空格和制表符。
# for循环遍历
with open('myfile.txt','r',encoding='utf-8') as fs:
for line in fs.readlines():
line=line.strip()
print(line)
3、文件内容的拼接:
# 打开第一个文件进行读取
with open('file1.txt', 'r', encoding='utf-8') as file1:
content1 = file1.read()
# 打开第二个文件进行读取
with open('file2.txt', 'r', encoding='utf-8') as file2:
content2 = file2.read()
# 将两个文件的内容拼接在一起
combined_content = content1 + content2
# 打开一个新文件进行写入
with open('combined.txt', 'w', encoding='utf-8') as combined_file:
combined_file.write(combined_content)
4、文件路径的拼接
import os
# 定义文件所在的文件夹和文件名
folder_path = "/path/to/folder"
file_name = "example.txt"
# 使用 os.path.join() 拼接文件路径
file_path = os.path.join(folder_path, file_name)
# 打开文件进行读取或写入等操作
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# 在这里可以进行文件内容的处理
# ...
# 如果需要拼接文件路径并创建文件夹,也可以这样做:
new_folder_path = "/path/to/new/folder"
new_file_path = os.path.join(new_folder_path, file_name)
# 打开新文件进行写入等操作
with open(new_file_path, 'w', encoding='utf-8') as new_file:
new_file.write("New content goes here")
最后
如果本篇文章能帮助到你的话还请点个赞加个关注,你的支持就是我创作的最大动力!