Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python TypeError: must be encoded string without NULL bytes, not str

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)
like image 911
rakitin Avatar asked Sep 25 '12 21:09

rakitin


1 Answers

"...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'
like image 193
Demian Avatar answered Sep 19 '22 16:09

Demian