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: path.basename and ntpath.basename functions returns an incorrect file name in Windows 7
Type: behavior Stage: resolved
Components: Windows Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Alex.Ternovoy, brian.curtin, loewis, tim.golden, vstinner
Priority: normal Keywords:

Created on 2013-02-23 13:21 by Alex.Ternovoy, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg182739 - (view) Author: Alex (Alex.Ternovoy) Date: 2013-02-23 13:21
1. I created file ("C:\Users\Alkor\Desktop\a3434.raw") on my desktop 
2. Tried to get the file name from the absolute path

Actual result:
C:\Users\Alkor>python
Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print os.path.basename ("C:\Users\Alkor\Desktop\a3434.raw")
Desktop3434.raw

The same for ntpath module:
>>> import ntpath
>>> print ntpath.basename ("C:\Users\Alkor\Desktop\a3434.raw")
Desktop3434.raw

Expected result:
a3434.raw

Environment:
Windows 7 x64 SP1 Ultimate
python 2.7.3150 (64-bit)
msg182740 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-02-23 13:23
> print os.path.basename ("C:\Users\Alkor\Desktop\a3434.raw")

Ah, it's a common trap of the Python syntax (and PHP, and C, and ... languages). "\" is a special character, you have to escape it: "\\".

"C:\\Users\\Alkor\\Desktop\\a3434.raw"

or simply use the "raw" string syntax:

r"C:\Users\Alkor\Desktop\a3434.raw"
History
Date User Action Args
2022-04-11 14:57:42adminsetgithub: 61482
2013-02-23 15:19:21eric.smithsetstatus: open -> closed
stage: resolved
2013-02-23 13:23:42vstinnersetresolution: not a bug

messages: + msg182740
nosy: + vstinner
2013-02-23 13:21:24Alex.Ternovoycreate