文章目录
前言
一、删除文件夹指定后缀名的文件
def delete_tif_files(folder_path):
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.tif'):
os.remove(os.path.join(root, file))
if os.path.getsize(result_path):
delete_tif_files(result_path)
1.os.path.getsize()获取文件大小
2.os.walk()遍历输出path路径下的所有root,dirs,files
:
os.mkdir(path)
三、复制文件夹以及子文件夹下所有文件
import os
import shutil
def copy_file(file_path,copy_path):
if not os.path.exists(copy_path):
os.mkdir(copy_path)
os.chdir(file_path)
print(os.path.abspath(os.curdir))
all_file = os.listdir()
for f in all_file:
if os.path.isdir(f):
file = os.path.join(copy_path, f)
if not os.path.exists(file):
os.mkdir(file)
file_path_new=os.path.join(file_path,f)
copy_path_new=os.path.join(copy_path,f)
check_file(file_path_new,copy_path_new)
os.chdir(file_path)
else:
file_old=os.path.join(file_path,f)
copy_path_new=os.path.join(copy_path,f)
if not os.path.isfile(copy_path_new):
shutil.copy(file_old, copy_path_new)
file_path=r""
copy_path=r''
file_list = copy_file(file_path,copy_path) #待读取的文件夹