Python TypeError: string should be encoded without NULL bytes, not str

Trying to get to know the python standard library and make some of them using my Windows machine. Using python 2.7 I have the following small script that is designed to search the directory and rename all the files in it after removing the digits from the file name. I get a typeerror that says: "the string should be encoded without NULL bytes, not str"

it calls lines 5 and 18, indicated below , where im uses 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) 
+7
source share
3 answers

turns out to be the only backslash problem. I thought os.path.normpath would format the path as os required.

+1
source

"... Photos \ Modified \ 0-PyTest"

Takes \ 0 as a null character. You must exit \ with \\ or simply put r in front of the line to make it raw:

 r'C:\Users\me\Photo Projects\Project Name\Project Photos\Modified\0-PyTest' 
+10
source

If you specify the URL of the path, just add r in front of it:

(r'E: \ Images \ 1.png ')

0
source

All Articles