文章目录
一、实验目的
掌握Python文件操作
二、实验环境
- 系统环境 : Windows 10 2004
- IDE : Pycharm
- Python: Python 3.8
三、文件操作
文件路径问题
在Windows中因为 " \ "被当成了转义字符,会导致路径找不到
例如 : C:\Users\xxx\Desktop\x.txt需改写为以下三种形式之一
r’C:\Users\xxx\Desktop\x.txt’
‘C:\Users\xxx\Desktop\x.txt’
‘C:/Users/xxx/Desktop/x.txt’
3.1 创建文件
3.1.1 当前项目路径
import os
def create_file(file_name):
# 获取当前路径
path = os.getcwd()
# 获得文件的路径
file_path = os.path.join(path,file_name)
# 如果该文件存在
if os.path.exists(file_path):
print('该文件已经存在')
return
# 创建文件
f = open(file_path,'w',encoding='utf-8')
if os.path.exists(file_path):
f.close()
print('%s 文件创建成功'%file_name)
return
print('%s 文件创建失败' % file_name)
3.1.2 指定路径
仅需将3.1.1中的path变量的值换为指定的路径值即可,需注意文件路径的问题
3.2 修改文件
3.2.1 写入内容
def write_file(file_name):
path = os.getcwd()
# 获得文件的路径
file_path = os.path.join(path, file_name)
with open(file_path, 'w',encoding='utf-8') as f:
data = input('输入内容:')
f.write(data)
f.close()
3.2.2 追加内容
def add_file_data(file_name):
path = os.getcwd()
file_path = os.path.join(path, file_name)
with open(file_path, 'a', encoding='utf-8') as f:
data = input('输入追加的内容:')
f.write('%s\n'%data)
f.close()
- with open中的参数a代表追加内容
- '\n’实现换行
3.2.3 覆盖内容
与3.2.1写入文件内容的代码一致
3.2.4 重命名
def rename_file(old_name,new_name):
path = os.getcwd()
old_path = os.path.join(path, old_name)
new_path = os.path.join(path, new_name)
os.rename(old_path, new_path)
3.3 读取文件
3.3.1 整体读取
def read_file(file_name):
path = os.getcwd()
# 获得文件的路径
file_path = os.path.join(path, file_name)
with open(file_path, 'r', encoding='utf-8') as f:
data = f.read()
print(data)
f.close()
3.3.2 逐行读取
修改3.3.1代码为
with open(file_path, 'r', encoding='utf-8') as f:
data = f.readlines()
for i in data:
print(i)
3.4 移动/复制文件
移动文件
import os
import shutil
def move_file(source_path, target_path):
if not os.path.isfile(source_path):
print('待移动文件不存在')
return
file_path, file_name = os.path.split(target_path)
if not os.path.exists(file_path):
os.makedirs(file_path)
shutil.move(source_path, target_path)
print('移动成功')
if __name__ == '__main__':
source_path = 'D:/Code/Python/File/newFiles.txt'
target_path = 'D:/Move/file.txt'
move_file(source_path, target_path)
复制文件
将shutil.move()换为shutil.copyfile()即可
shutil.copyfile(source_path, target_path)
3.5 删除文件
def delete_file(source_path):
if not os.path.exists(source_path):
print('待删除文件不存在')
return
os.remove(source_path)
四、文件夹操作
4.1 读取
4.1.1 显示文件夹中文件
def display_all_files(folder_path):
for i in os.listdir(folder_path):
path = os.path.join(folder_path, i)
# 如果是文件
if os.path.isfile(path):
print(path)
4.1.2 递归显示文件夹中文件
def display_all_folders(folder_path):
for i in os.listdir(folder_path):
path = os.path.join(folder_path, i)
# 如果该对象是文件夹
if os.path.isdir(path):
print("--folder : %s"%i)
display_all_files(path)