8

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)
1

4 Answers 4

12

"...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'
Sign up to request clarification or add additional context in comments.

Comments

1

turns out to be the single backslash problem. i thought os.path.normpath would format the path as required by the os.

Comments

0

If you are giving a path url just add r before it :

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

1 Comment

Hi, welcome to SO! This question is very old and has already been answered with a very similar reply, so there's no need to post it again. There'll no doubt be other people with new questions that need your help though.
0

This Link for DBL Need to be fixed from String Error.

subObject: Traceback (most recent call last):
  File "D:\noesisv4395\plugins\python\fmt_DBL.py", line 32, in LoadModel
    readObject(bs)
  File "D:\noesisv4395\plugins\python\fmt_DBL.py", line 137, in readObject
    print("subObject:",name, info, bs.getOffset())
TypeError: must be str without null bytes, not str

1 Comment

do Notify me if there's a Solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.