Issue
I have a simple renaming script I'm running in a test directory. It works fine for the first replace method but throws an error on the second use of the method. Can anyone help me solve this one?
dir = ('C://Users//zstanley//OneDrive - Golden Gate National Parks Conservancy//BolinasWyeWetlands')
os.chdir(dir)
[os.rename(os.path.join(dir, f), os.path.join(dir, f).replace('_', '').replace(' ', '').lower())
for f in os.listdir(dir)]
Solution
This works (with \\
for windows):
Modifications:
- remove the list comprehension (because it was a bit too long).
- remove
os.chdir(dir)
, no need to change directory.
I was then able to easily inspect each file and run on a test folder.
import os
dir = ("C:\\test")
# check that the folder exists
if os.path.isdir(dir):
print('folder exists')
else:
print('folder does not exist')
exit()
for f in os.listdir(dir):
a = os.path.join(dir, f)
b = os.path.join(dir, f).replace('_', '').replace(' ', '').lower()
os.rename(a, b)
print('file names updated')
Here are the results:
file system before:
file system after:
Answered By - D.L
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.