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.split fails on windows path
Type: behavior Stage:
Components: Windows Versions: Python 3.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Hanz, SilentGhost, christian.heimes, r.david.murray
Priority: normal Keywords:

Created on 2013-11-30 16:56 by Hanz, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (6)
msg204826 - (view) Author: Hanz Kanst (Hanz) Date: 2013-11-30 17:06
os.path.split fails on windows path
to reproduce in python 3.3:

file = "C:\progs\python\test\target\Amy Winehouse\Amy Winehouse - Back To Black (2006)\01 - Rehab.ogg"
os.path.split(os.path.abspath(file))[0]
returns
'C:\\progs\\python\testordner\target\\Amy Winehouse'
and
os.path.split(os.path.abspath(file))[1]
returns
'Amy Winehouse - Back To Black (2006)\x01 - Rehab.ogg'

According to the definition the tail should never contain a tail.
msg204827 - (view) Author: Hanz Kanst (Hanz) Date: 2013-11-30 17:07
According to the definition the tail should never contain a slash.
msg204828 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2013-11-30 17:14
file must be a raw string:

file = r'C:\progs\python'

Then everthing works.
msg204830 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-11-30 17:18
Ah, you fell victim to a classic gotcha. :)
Either you have to quote \ with \\ or you have to use a raw string. Withouth a raw string \t is TAB and \01 is the byte \x01:

>>> import ntpath
>>> fname = r"C:\progs\python\test\target\Amy Winehouse\Amy Winehouse - Back To Black (2006)\01 - Rehab.ogg"
>>> ntpath.split(fname)
('C:\\progs\\python\\test\\target\\Amy Winehouse\\Amy Winehouse - Back To Black (2006)', '01 - Rehab.ogg')

>>> len("\01")
1
>>> "\01" == chr(1)
True
>>> len(r"\01")
3
msg204831 - (view) Author: Hanz Kanst (Hanz) Date: 2013-11-30 17:50
Hm, how can I handle this if "file" is an existing string and there is no option to assign raw via r'some\raw\string'?
msg204839 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-11-30 19:06
If it is an existing string, the backslashes are already in the string.  The r prefix or the escaping is only required to get the backslashes into a string when you are coding them into a source file.
History
Date User Action Args
2022-04-11 14:57:54adminsetgithub: 64043
2013-11-30 19:06:02r.david.murraysetnosy: + r.david.murray
messages: + msg204839
2013-11-30 17:50:54Hanzsetmessages: + msg204831
2013-11-30 17:18:50christian.heimessetstatus: open -> closed
nosy: + christian.heimes
messages: + msg204830

2013-11-30 17:14:15SilentGhostsetresolution: not a bug

messages: + msg204828
nosy: + SilentGhost
2013-11-30 17:07:32Hanzsetmessages: + msg204827
2013-11-30 17:06:09Hanzsetmessages: + msg204826
2013-11-30 16:56:21Hanzcreate