【发布时间】:2022-01-09 09:44:30
【问题描述】:
从用户提供的源和目标复制文件后,需要根据创建时间重命名文件。这些文件有多种扩展类型,在重命名时需要保留,并记录原始文件名。在将文件复制到目标位置后尝试重命名文件时会出现此问题。所有尝试的解决方案都会在回调中创建异常。这里最好的方法是什么?
def filter_copy(self):
current_dir = self.Source.get()
destination_dir = self.Destination.get()
extensions = (['.avi', '.mpg'])
os.chdir(current_dir)
for root, dir, files in os.walk('.'):
for file in files:
if os.path.splitext(file)[1] in extensions:
src = os.path.join(root, file)
time.sleep(0.1)
logging.info("Copying the file %s..." % file)
print("Copying the file %s..." % file)
try:
with open('OriginalFilesReceipt.txt', 'w') as f:
f.write(file)
except FileNotFoundError:
logging.info("The directory does not exist to create to generate a receipt")
shutil.copy(src, destination_dir)
for names in os.listdir('.'):
t = os.path.getmtime(names)
v = datetime.datetime.fromtimestamp(t)
x = v.strftime('%Y%m%d-%H%M%S')
os.rename(names, x)
【问题讨论】:
-
您的问题与
tkinter无关,因此您应该创建一个不使用它的 [rme] 并描述您想要的输入和预期输出 - 但大概没有得到 (如果你知道原因,为什么会这样)。
标签: python filesystems