import os
import re
import time
import shutil
Project = 'X1D'
# 指定目录
target_directory = '.'
def creatfolder():
Project = input('eg: Project:X1D(or X1D_0610)\nProject:')
startindex = input('First-Index:')
endindex = input('End-Index:')
for i in range( int(startindex), int(endindex)+1):
folder_name = 'S' + str(i) + '_' + Project
folder_path = os.path.join(target_directory, folder_name)
os.makedirs(folder_path)
def rename():
# 指定目录
target_directory = "."
# 遍历指定目录下的所有文件
for root, dirs, files in os.walk(target_directory):
for file in files:
if file.endswith('.txt') or file.endswith('.bmp') or file.endswith('.raw') or file.endswith('.jpg') or file.endswith('.jpeg'):
if str(os.path.basename(root)) in file : continue
if 'S' in file.split("_")[0] : continue
if Project in file : continue
# 获取文件所在文件夹的名称
folder_name = os.path.basename(root)
if file.endswith('.bmp') and 'Gain' in file:
new_file_name = folder_name + '.bmp'
else:
old_file_postname = re.sub(r'^.*?_', '', file)
new_file_name = folder_name + "_" + old_file_postname
# 新文件名,将文件夹名称和原文件名拼接起来,并去掉第一个 "_" 之前的内容
# 原文件路径
old_file_path = os.path.join(root, file)
# 新文件路径
new_file_path = os.path.join(root,new_file_name)
# 重命名文件
os.rename(old_file_path, new_file_path)
print('Folder:%s, Oldfile:%s, New_file_name:%s'%(os.path.basename(root),file,new_file_name))
'''
# 获取当前最大文件夹名字的数字
max_digit = 0
for filename in os.listdir(target_directory):
if filename.isdigit():
digit = int(filename)
if digit > max_digit:
max_digit = digit
'''
def rename_imagelib():
# 指定目录
target_directory = "."
# 遍历指定目录下的所有文件
for root, dirs, files in os.walk(target_directory):
for file in files:
if file.endswith('.txt') or file.endswith('.bmp') or file.endswith('.raw') or file.endswith('.jpg') or file.endswith('.jpeg'):
if str(os.path.basename(root)) in file : continue
if 'S' in file.split("_")[0] : continue
if Project in file : continue
# 获取文件所在文件夹的名称
folder_name = os.path.basename(root)
folder_name_pre = folder_name.split('_', 1)[0]
folder_name_pre = folder_name_pre.strip('S')
if folder_name_pre == file.split('_', 1)[0]: continue
old_file_postname = re.sub(r'^.*?_', '', file)
new_file_name = folder_name_pre + "_" + old_file_postname
# 新文件名,将文件夹名称和原文件名拼接起来,并去掉第一个 "_" 之前的内容
# 原文件路径
old_file_path = os.path.join(root, file)
# 新文件路径
new_file_path = os.path.join(root,new_file_name)
# 重命名文件
os.rename(old_file_path, new_file_path)
print('Folder:%s, Oldfile:%s, New_file_name:%s'%(os.path.basename(root),file,new_file_name))
def copyforvrn():
# 指定要移动的文件类型
file_extension = ".txt"
# 指定要移动的文件所在目录
source_directory = "."
# 指定要移动到的目标目录
target_directory = "copyforvrn"
# 遍历根目录下的所有文件夹
for root, dirs, files in os.walk(source_directory):
# 遍历当前文件夹中的所有文件
for file in files:
# 如果文件扩展名符合要求,则移动文件到目标目录中
if file.endswith('.txt') or file.endswith('.bmp')or file.endswith('.raw'):
if 'After'in file or file.endswith('.jpg') :continue
# 构造文件的完整路径
source_path = os.path.join(root, file)
#target_path = os.path.join(target_directory, os.path.relpath(root, source_directory), file)
target_path = os.path.join(target_directory, file)
# 创建目标文件夹(如果不存在)
target_folder = os.path.dirname(target_path)
if not os.path.exists(target_folder):
os.makedirs(target_folder)
# 移动文件
#shutil.move(source_path, target_path)
shutil.copy(source_path, target_path)
print(file)
def copyforComBMP():
# 指定要移动的文件所在目录
source_directory = "."
# 指定要移动到的目标目录
target_directory = "compare"
# 遍历根目录下的所有文件夹
for root, dirs, files in os.walk(source_directory):
# 遍历当前文件夹中的所有文件
for file in files:
folder_name = os.path.basename(root)
if target_directory in folder_name:continue
# 如果文件扩展名符合要求,则移动文件到目标目录中
if file.endswith('.bmp'):
if 'L.bmp' in file or 'M.bmp' in file or 'S.bmp' in file or 'VS.bmp' in file:continue
# 构造文件的完整路径
source_path = os.path.join(root, file)
#target_path = os.path.join(target_directory, os.path.relpath(root, source_directory), file)
target_path = os.path.join(target_directory, file)
# 创建目标文件夹(如果不存在)
target_folder = os.path.dirname(target_path)
if not os.path.exists(target_folder):
os.makedirs(target_folder)
# 移动文件
#shutil.move(source_path, target_path)
shutil.copy(source_path, target_path)
print(file, folder_name)
def removetxtspace():
# 指定要处理的文件夹路径
folder_path = "."
for root, dirs, files in os.walk(folder_path):
for file in files:
# 只处理txt文件
if file.endswith(".txt"):
# 构造文件的完整路径
file_path = os.path.join(root, file)
# 打开文件并读取内容
with open(file_path, "r") as f:
#content = f.readlines()
content = f.read()
# 删除每行末尾的空格
content = [line.rstrip().rstrip('\n') + '\n' for line in content.split('\n')]
#content = [line.rstrip().rstrip("\n") for line in content]
#content = [line.rstrip() for line in content]
# 将处理后的内容写回到文件中
with open(file_path, "w") as f:
f.writelines(content)
try:
# main
if __name__ == "__main__":
func=input('input:1: 创建文件夹 // input:2 重命名 // input:3 按原始格式重命名并删除.txt中行尾的空格 // input:4 移动指定文件至copyforvrn文件夹\ninput:')
if func == '1':
creatfolder()
print('done')
elif func == '2':
rename()
copyforComBMP()
print('done')
elif func == '3':
rename_imagelib()
removetxtspace()
print('done')
elif func == '4':
copyforvrn()
print('done')
else:
print('none')
#input('Press Any Key to End')
#time.sleep(10)
os.system("pause")
except Exception as e:
# 打印错误信息
print('发生错误:', e)
# 等待一段时间后退出程序
time.sleep(5)
exit()