Issue 4723: os.path.basename error on directory names with numbers (original) (raw)

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:43 admin set github: 48973
2008-12-23 00:01:41 loewis set messages: +
2008-12-22 23:35:08 kle_py set messages: +
2008-12-22 17:52:01 loewis set status: open -> closedresolution: not a bugmessages: + nosy: + loewis
2008-12-22 16:53:52 tim.golden set nosy: + tim.goldenmessages: +
2008-12-22 16:37:34 kle_py create