Reputation: 21
I have a simple script that runs through a directory and renames the files.
Junk.Old.txt > NEW_FILE_DATE_TIME_Junk.txt
If the files match, it works perfectly.
Problem I have is if a file has already been renamed, and I have a new file(s) I want to rename, I get an error. So if I have two files in the new format and one to rename, it fails with below and doesn't rename the old file.
Traceback (most recent call last):
File "./rename.py", line 17, in <module>
f_job, f_ext = f_name.split('.print')
ValueError: not enough values to unpack (expected 2, got 1)
Obviously it is seeing the already renamed files and quitting. So what do I need in order for it to check if file format already exists, skip to next until all files are renamed?
#!/bin/python3
import os
os.chdir('/my/folder/')
for f in os.listdir():
f_name, file_ext = os.path.splitext(f)
f_job, f_ext = f_name.split('.Old')
f_attrib1 = "NEW"
f_attrib2 = "FILE"
import time
new_name = '{}_{}_{}_{}_{}{}'.format(f_attrib1, f_attrib2, time.strftime("%Y%m%d"), time.strftime("%H%M%S"), f_job, file_ext)
os.rename(f, new_name)
Upvotes: 0
Views: 360
Reputation: 116
If you just want to ignore files that don't have ".Old" in them, you can add this above your split:
if '.Old' not in f_name:
continue
Upvotes: 1