python实现对文件操作
查询文件夹的指定格式文件
例如获取jpg文件
def get_file(dir):
folders = [dir]
results = []
specify_str = r'.jpg' # 用来过滤的
# 把目录下所有文件夹存入待遍历的folders
for folder in folders:
folders += [os.path.join(folder, x) for x in os.listdir(folder) \
if os.path.isdir(os.path.join(folder, x))]
# 把所有满足条件的文件的相对地址存入结果results 有过滤的情况
results += [os.path.relpath(os.path.join(folder, x), start=dir) \
for x in os.listdir(folder) \
if os.path.isfile(os.path.join(folder, x)) and specify_str in x]
r = []
for result in results:
r += [os.path.join(dir, result)]
return r
删除指定文件
def delete_jpg(files):
for file in files:
os.remove(file)
获取指定文件夹里指定文件的所有文件名
def save_excel(target_file_path, save_path):
# 创建一个新的工作簿wb
gf_files = get_files(target_file_path)
file_name_list = get_files_name(gf_files)
# print(file_name_list)
wb = workbook.Workbook()
ws = wb.active # 获取当前正在操作的表对象
for file_name in file_name_list:
ws.append([file_name])
wb.save(save_path)
# 获取所有文件
def get_files(dir):
folders = [dir]
results = []
# 把目录下所有文件夹存入待遍历的folders
for folder in folders:
folders += [os.path.join(folder, x) for x in os.listdir(folder) \
if os.path.isdir(os.path.join(folder, x))]
# 把所有满足条件的文件的相对地址存入结果results 有过滤的情况
results += [os.path.relpath(os.path.join(folder, x), start=dir) \
for x in os.listdir(folder) \
if os.path.isfile(os.path.join(folder, x))]
r = []
for result in results:
r += [os.path.join(dir, result)]
return r
# 获取所有名字
def get_files_name(file_list):
specify_str = r'MSS' # 用来过滤的
specify_str2 = r'.dat' # 用来过滤的
specify_str3 = r'.enp' # 用来过滤的
filename_list = []
for file in file_list:
if specify_str in file and specify_str2 in file and specify_str3 not in file:
index_start = find_last(file, '\\')
index_stop = find_last(file, '-')
filename = file[index_start + 1:index_stop]
filename_list.append(''.join(filename))
return filename_list
比较两个表格中不同的行,将结果保存为表格
def differ_excel(exc1,exc2,save_path):
excl1 = xlrd.open_workbook(exc1)
sheet1 = excl1.sheet_by_index(0)
row_num1 = sheet1.nrows
data1 = []
for i in range(0, row_num1):
row_value_list = sheet1.row_values(i)
data1.append(row_value_list)
excl2 = xlrd.open_workbook(exc2)
sheet2 = excl2.sheet_by_index(0)
row_num2 = sheet2.nrows
data2 = []
for i in range(0, row_num2):
row_value_list = sheet2.row_values(i)
data2.append(row_value_list)
wb = workbook.Workbook()
ws = wb.active # 获取当前正在操作的表对象
for i in data1:
for index, j in enumerate(data2):
if i[0] == j[0]:
break
else:
if index == len(data2) - 1:
ws.append([i[0]])
print([i[0]])
wb.save(save_path)
根据表格移动文件夹中的文件
# 移动文件
def move_file_withExcl(file_path,target_dir,move_dir):
excl = xlrd.open_workbook(file_path)
sheet = excl.sheet_by_index(0)
row_num = sheet.nrows
# specify_str = r'PAN' # 用来过滤的
data = []
for i in range(0, row_num):
row_value_list = sheet.row_values(i)
data.append(row_value_list)
filelist = get_files(target_dir)
print(len(filelist))
for x in data:
for files in filelist:
index = x[0].find('-')
filename = x[0][0:index]
# print(filename)
# filename = x[0]
# if filename in files and specify_str in files:
if filename in files:
# shutil.move(files, move_dir)
# os.remove(files)
copy(files, move_dir)
else:
continue
return data
复制文件
# 复制文件
def copy_file(filelist,to_path):
for filename in filelist: # 遍历pathDir下的所有文件filename
copy(filename, to_path) #
移动文件
# 移动文件
def move_file(filelist, target_dir):
specify_str = r'PAN' # 用来过滤的
specify_str2 = r'.tiff' # 用来过滤的
for files in filelist:
if specify_str2 in files and specify_str in files:
shutil.move(files, target_dir)
else:
continue
删除指定文件
# 删除指定文件
def delete_jpg(files):
for file in files:
os.remove(file)
|