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)
rakitin
  • 1,943
  • 6
  • 25
  • 51
  • This link I followed which solved my error https://stackoverflow.com/questions/1254454/fastest-way-to-convert-a-dicts-keys-values-from-unicode-to-str – Santosh kumar Manda Feb 20 '19 at 07:36

4 Answers4

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'
Obscure Geek
  • 749
  • 10
  • 22
Demian
  • 121
  • 1
  • 3
1

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

rakitin
  • 1,943
  • 6
  • 25
  • 51
0

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

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

0x Tps
  • 165
  • 1
  • 1
  • 9
  • 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. – Tom Dec 21 '16 at 11:28
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