python - 如何识别 Project_Code、File_Name、File_Format。并基于此创建新的文件夹结构
问题描述
我不是一个好的 python 程序员,而是一个好的 max scripter。我一直在尝试自动化清理损坏的最大文件的过程。总共有 88000 个文件需要清理。
我要清理的文件是 .zip 格式,具有这样的命名约定。
“Project_Name_File_Name_File_Format.zip”
加载 .max 文件和清理损坏的自动化过程是通过 max 脚本完成的。
我一直在尝试做的是像这样创建一个文件夹结构:
项目名称--> 文件名--> 文件格式
过去两周我一直在努力,在这方面仍然没有很好的进展。
这是我一直试图让它工作的基本代码,至少可以用文件格式识别文件名。我尝试了项目名称和文件格式的字典方法。仍然没有运气,我创建了一个字符串列表,然后我进入了一个创建字符串列表和字符串列表的循环。
import os
files = os.listdir('path\\') # Set location where all the .zip files are present.
files_zip = [i for i in files if i.endswith('.zip')]
for file_name in files_zip:
print(file_name)
token = os.path.splitext(file_name)[0].split("_")
#print(token)
new_token = token[1:-1]
print(new_token)
new_file_name = "_".join(new_token)
print(new_file_name)
我尝试做的另一组代码是here
import os
path = 'path\\'
files = os.listdir(path) # Set location where all the .zip files are present.
# Dictionary Project Keys and Values
project_dic = {'ABC': 'Apple Bucket Cake', 'XYZ': 'Xerox Yacht Zoo'}
# Dictionary for File Formats
file_formats = {'FBX': 'FBX', 'OBJ': 'OBJ', '3ds Max': '3ds Max'}
# Looking for the files which ends with Project Names (prj_lst)
files_txt = [i for i in files if i.endswith('.zip')]
# print(files_txt)
prj_lstToStr = ' '.join([str(elem) for elem in files_txt])
name_set = prj_lstToStr.split('prj_lstToStr')
# print(name_set)
#print("Project_List : " + str(name_set))
res = [ele if len(ele) > 0 else () for ele in [[key for key in project_dic if key in sub] for sub in name_set]]
#print("Project_Matching_Keys : " + str(res))
string_key = ''.join(str(res))
format_list = [ele if len(ele) > 0 else () for ele in [[key for key in file_formats if key in sub] for sub in name_set]]
#print("Format_Matching_Keys : " + str(format_list))
format_key = ''.join(str(format_list))
token = files_txt
解决方案
我不知道你到底想做什么,所以如果它不正确,我会删除这个答案。据我了解:
input = ["BC_Warren_Vidic_Head_OBJ.zip",
"ALS_Sand_Mound_pr_ann_0479_c_OBJ.zip",
"GRT_ENV-SPE-GRP-SK-ExplorationChestPMC-A_3dsMax.zip",
"KLS_alpha-GEN_PRO_HedgePotPlanter_Group_01A_2021-03-31_FBX.zip",
"MISC_gho_caucasian-mattE_(wise)_OBJ.zip",
"MISC_W_ATT_SalvoXL_FBX.zip",
"MISC_XA-20_Razorback_JetFighter_3dsMax.zip",
"WLD_ENV-GLO-PRO-Bivouac-TacticalSmartphone-A_3dsMax.zip",
"XYZ_WPN_ATT_MAG_MagpulPMAGMOE_FBX.zip"]
for inp in input:
splitted = inp.split('_')
project_name = splitted[0]
file_name = '_'.join(splitted[1:-1])
file_format = splitted[-1].split('.')[0]
path = f'./{project_name}/{file_name}/{file_format}'
os.makedirs(os.path.dirname(path), exist_ok=True)
推荐阅读
- nearest-neighbor - MetPy 的社区
- python - Go build 不会从脾气暴躁的 go 代码生成二进制文件?
- php -
- tensorflow - 对于 62 个类的对象检测模型,我应该采取多少训练样本?
- python - Pandas:通过两个分隔符将列拆分为多列
- powershell - 如何使用 Powershell 3.0 注释掉 XML 节点?
- file - 是否有多个文件的 :tabnew ?
- csv - 我可以使用 Text::CSV_XS 解析 csv 格式的字符串而不将其写入磁盘吗?
- python - 多维对角矩阵,其中对角元素是行向量或列向量
- powershell - 转换证书时 PowerShell 中的 Openssl 错误