This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: os.path.basename error on directory names with numbers
Type: behavior Stage:
Components: Windows Versions: Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: kle_py, loewis, tim.golden
Priority: normal Keywords:

Created on 2008-12-22 16:37 by kle_py, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg78197 - (view) Author: Klemens Häckel (kle_py) Date: 2008-12-22 16:37
I am using python for some backup tasks, and recently i found a problem
whe i have certain directory names.
Probably the problem is only in windows, however i would like You to know.
I verified the same behaviour in (WindowsXP, spanish, SP3) using Python
2.5.2 and also in 2.6.1 (installers from Python.org).

In my case i have directory names, beginning with year/month numbers.
Aparrently python is confusing something, so os.path.basename is not
working:

dd = 'C:\downloads\hacking\0812logcompress'
>>> os.path.basename(dd)
'hacking\x00812logcompress'
>>> dd = 'C:\downloads\hacking\logcompress'
>>> os.path.basename(dd)
'logcompress'
msg78198 - (view) Author: Tim Golden (tim.golden) * (Python committer) Date: 2008-12-22 16:53
You need to use raw strings or to use forward-slashes in your pathnames:

r"c:\downloads\hacking\0812logcompress"

or

"c:/downloads/hacking/0812logcompress"

The sequence \0 has a special meaning in strings, introducing an octal
escape, I think.
msg78200 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2008-12-22 17:52
Tim is right; \0 is an octal escape (for the byte with the ordinal value 0).
msg78217 - (view) Author: Klemens Häckel (kle_py) Date: 2008-12-22 23:35
Of course my example was not that trivial, since i do sort of
directory-walking, and i get the value passed over. And in Windows it is
with backslash!

Ok, i will check my code, wherever i use os.path.basename and change it
to process as raw string.
Thank You
msg78219 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2008-12-23 00:01
> Of course my example was not that trivial, since i do sort of
> directory-walking, and i get the value passed over. And in Windows it is
> with backslash!

Not necessarily. Windows supports forward slashes as path separators
just as well.

> Ok, i will check my code, wherever i use os.path.basename and change it
> to process as raw string.

You can consider avoiding the path separator by using os.path.join all
the time. There might still be places where you need to provide absolute
file names as literals in your source code, but those should be few.
History
Date User Action Args
2022-04-11 14:56:43adminsetgithub: 48973
2008-12-23 00:01:41loewissetmessages: + msg78219
2008-12-22 23:35:08kle_pysetmessages: + msg78217
2008-12-22 17:52:01loewissetstatus: open -> closed
resolution: not a bug
messages: + msg78200
nosy: + loewis
2008-12-22 16:53:52tim.goldensetnosy: + tim.golden
messages: + msg78198
2008-12-22 16:37:34kle_pycreate