The script below renames all files in a directory, to the id in the second found string, starting with ProcID:
:
1. Renaming to the id in the second matching string (literally; no extension)

#!/usr/bin/env python3
import shutil
import os
import sys
dr = sys.argv[1]
for f in os.listdir(dr):
file = os.path.join(dr, f)
try:
# only rename if the second instance exists
new = [s for s in open(file).read().split() if s.startswith("(ProcID:")][1]
except (IndexError, IsADirectoryError):
pass
else:
# rename the file
shutil.move(file, os.path.join(dr, new.split(":")[-1].strip()[:-1]))
To use
- Copy the script into an empty file, save it as
rename_files.py
run it with the directory as argument:
python3 /path/to/rename_files.py /path/to/directory_with_files
Notes
- The script assumes the (exact) format:
ProcID:0000014778
- The script will not rename files if there is no second instance of
ProcID
(or none at all)
2. Only rename .txt files, and keep the extension
...Then use the version below, it will only rename .txt
files and keep the extension in the renamed file. Usage is exactly the same.
#!/usr/bin/env python3
import shutil
import os
import sys
dr = sys.argv[1]
for f in os.listdir(dr):
file = os.path.join(dr, f)
try:
# only rename is the second instance exists
new = [s for s in open(file).read().split() if all([
s.startswith("(ProcID:"), f.endswith(".txt")
])][1]
except (IndexError, IsADirectoryError):
pass
else:
shutil.move(file, os.path.join(dr, new.split(":")[-1].strip()[:-1]+".txt"))
What the script does
it lists the files in the directory:
for f in os.listdir(dr)
lists the strings inside the file, starting with ProcID:
new = [s for s in open(file).read().split() if s.startswith("(ProcID:")][1]
The ending [1]
extracts the second occurrence of strings, starting with ProcID:
([0]
is the first one)
splits of the id:
new.split(":")[-1].strip()
and renames the file, using the id:
shutil.move(file, os.path.join(dr, new.split(":")[-1].strip()[:-1]))
EDIT
Python2 version of script 1. OP turns out to have python2 installed, which requires IOError
instead of IsADirectoryError
to prevent errors in case the script runs into a directory instead of a file.
#!/usr/bin/env python
import shutil
import os
import sys
dr = sys.argv[1]
for f in os.listdir(dr):
file = os.path.join(dr, f)
try:
# only rename if the second instance exists
new = [s for s in open(file).read().split() if s.startswith("(ProcID:")][1]
except (IndexError, IOError):
pass
else:
# rename the file
shutil.move(file, os.path.join(dr, new.split(":")[-1].strip()[:-1]))
...and for being complete, a python2 version of the second script:
#!/usr/bin/env python
import shutil
import os
import sys
dr = sys.argv[1]
for f in os.listdir(dr):
file = os.path.join(dr, f)
try:
# only rename is the second instance exists
new = [s for s in open(file).read().split() if all([
s.startswith("(ProcID:"), f.endswith(".txt")
])][1]
except (IndexError, IOError):
pass
else:
shutil.move(file, os.path.join(dr, new.split(":")[-1].strip()+".txt"))
python-minimal
orpython3-minimal
preinstalled, though I'm unsure if this is universally true on Ubuntu Core. Either way, you should be able to get a fully functional Python 3 installation by installing thepython3
package, for example by runningsudo apt-get update
and thensudo apt-get install python3
. – Eliah Kagan Jan 30 '17 at 13:23