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'
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.
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
> 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