Trying to get familiar with python's standard library and doing some mucking around with it on my Windows machine. Using python 2.7 I have the following little script which is intended to look in a directory and rename all of the files therein after removing numerals from the file name. I'm getting a typeerror that says "must be encoded string without NULL bytes, not str"
it calls out lines 5 and 18, noted below, where im using os.path.exists.
Any help would be greatly appreciated!
import os, re, string, glob
path = os.path.normpath('C:\Users\me\Photo Projects\Project Name\Project Photos\Modified\0-PyTest')
ln5:if os.path.exists(path):
print "path exists at " + path
for file in glob.glob(os.path.join(path, '*.jpg')):
new_path = os.path.join(os.path.dirname(file), re.sub('\d', '', os.path.basename(file)))
line18: if not os.path.exists(new_path):
os.rename(file, new_path)
"...Photos\Modified\0-PyTest"
Its taking the \0 as a null character. You have to escape \
using \\
, or just put an r
before the string to make it raw:
r'C:\Users\me\Photo Projects\Project Name\Project Photos\Modified\0-PyTest'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With