我目前正在处理一个脚本,以获取原始文本文件,将它们放置在基于版本号系统的正确年份文件夹中,并对其进行重命名,以便我们的开发人员可以轻松地将它们集成到我们的产品中。
有问题的文件称为APR30CaseRVU-1Day.txt。 我需要采用refAPRCaseRVU-1Day.txt的形式。
附加参考,删除30,保留1天。
该脚本对于没有此额外“ 1Day”的其他文件运行正常。我是一个初学者,所以我确定我的工作流程很烂,但我的想法是两次使用os.rename。一次删除字符,然后在连字符上拆分以将“ 1”重新添加到文件名中,但是当我在第一个os.rename之后打印文件名时,它仍然有30和1。
有任何技巧可以更有效地做到这一点吗?
folder = r"C:\Users\xx\Desktop\Python Final Project\Raw"
import os
import re
import shutil
for root, dirs, filenames in os.walk(folder):
for filename in filenames:
srcpath = os.path.join(root, filename)
#split the filename so that we can rename accordingly below, filename_split[0] = filename, filename_split[1] = file ext
filename_split = os.path.splitext(filename)
name = filename_split[0]
ext = filename_split[1]
newfolder = ''
destpath = os.path.join(newfolder, "ref" + re.sub(r'\d', '', filename_split[0]) + filename_split[1])
#first step: remove AP files entirely from directory
if filename.startswith("AP27"):
os.remove(os.path.join(folder, filename))
#the rest include a version number that must be routed to the correct year folder directory created above
elif filename.__contains__("30") and filename.__contains__("Day"):
newfolder = r"C:\Users\xx\Desktop\Python Final Project\Raw\2013"
os.rename(os.path.join(root, filename),
os.path.join(newfolder, "ref" + re.sub(r'\d', '', filename_split[0]) + filename_split[1]))
print(filename)
day_name = re.split(r'[\s-]+', filename)
print(day_name)
first_name = day_name[0]
last_name = day_name[1]
os.rename(os.path.join(newfolder, filename),
os.path.join(newfolder, "ref" + first_name + '1' + last_name))
答案 0 :(得分:1)
我自己解决了这个问题
os.rename(os.path.join(root, filename),
os.path.join(newfolder, "ref" + re.sub(r'\d{2}', '', name) + ext))
添加{2}使其寻找2个连续的数字,而将1个保留下来。
谢谢!