os.walk return hex excapes

A

Alex S

Hi,
os.walk return hex excape sequence inside a files name, and when i try
to feed it back to os.remove i get

OSError: [Errno 22] Invalid argument:
'C:\\Temp\\?p?\xbfS\xbf\xac?G\xaba ACDSee \xbb?a??n a???\xac\xb5\xbfn.exe'
 
L

Leo Kislov

Alex said:
Hi,
os.walk return hex excape sequence inside a files name, and when i try
to feed it back to os.remove i get

OSError: [Errno 22] Invalid argument:
'C:\\Temp\\?p?\xbfS\xbf\xac?G\xaba ACDSee \xbb?a??n a???\xac\xb5\xbfn.exe'

It's not escape sequences that are the problem but question marks, I
suspect. Most likely this file name contains characters not in your
locale's language. To access this file name you need to use unicode,
just make sure the first parameter of os.walk is a unicode string, for
example: os.walk(u'c:\\temp'). The exact code how to make the first
parameter unicode depends on where it is coming from (network, config
file, registry, etc...) Reading unicode tutorial is highly recommended.

-- Leo
 
M

Marc 'BlackJack' Rintsch

os.walk return hex excape sequence inside a files name, and when i try
to feed it back to os.remove i get

OSError: [Errno 22] Invalid argument:
'C:\\Temp\\?p?\xbfS\xbf\xac?G\xaba ACDSee \xbb?a??n a???\xac\xb5\xbfn.exe'

There is no hex escape in that file name, just in the representation you
get in the error message. The `repr()` form of a string contains just
ASCII, everything outside printable ASCII characters is printed as hex
escape so you can see what the string actually contains without being
interpreted by the shell, IDE or wherever the string is displayed.

How does the real file name look like?

Ciao,
Marc 'BlackJack' Rintsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top