在Python中,可以使用os
模块来处理文件夹里的文件。以下是一些常见的方法:
import os
folder_path = '/path/to/folder'
file_list = os.listdir(folder_path)
import os
folder_path = '/path/to/folder'
for file_name in os.listdir(folder_path):
file_path = os.path.join(folder_path, file_name)
# 进行相应的处理
import os
file_path = '/path/to/file'
if os.path.exists(file_path):
# 文件存在
else:
# 文件不存在
import os
folder_path = '/path/to/folder'
os.makedirs(folder_path, exist_ok=True)
import shutil
source_file = '/path/to/source_file'
destination_folder = '/path/to/destination_folder'
shutil.copy(source_file, destination_folder)
import os
file_path = '/path/to/file'
os.remove(file_path)
请根据具体需求选择适当的方法来处理文件夹中的文件。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:python怎么移动文件夹里的文件