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.

Title: abspath strips trailing spaces on win32
Type: behavior Stage: resolved
Components: Windows Versions: Python 2.7
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: Jeremy.Gray, brian.curtin, ezio.melotti, pitrou, tim.golden
Priority: normal Keywords:

Created on 2013-06-15 12:38 by Jeremy.Gray, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg191202 - (view) Author: Jeremy Gray (Jeremy.Gray) Date: 2013-06-15 12:38
The behavior of os.path.abspath differs between Mac OS X and Windows. It seems like a bug that, on Windows, abspath strips trailing whitespace:
>>> from os.path import abspath
>>> abspath('start  ') + 'END'
>>> from os.path import abspath
>>> abspath('start  ') + 'END'
'/Users/jgray/code/start  END'

I have only tested with python 2.7.3 (Enthought distribution), 32-bit, and only Mac 10.8.2 and Windows 7.
msg191233 - (view) Author: Jeremy Gray (Jeremy.Gray) Date: 2013-06-15 21:00
Maybe this a documentation / run-time warning issue, rather than bug. Trailing spaces in filenames are illegal in Win32; nonetheless they can occur, because there are instructions on how to remove such files: So os.path.abspath() not only returns the absolute path of a (hypothetical) file, it is also ensuring that the file name is legal within the operating system. It does so silently. (I say hypothetical because there's no requirement that the file actually exists.)

This behavior from python was surprising since "explicit is better than implicit": I was not getting just an absolute path, but a changed filename (causing a file-not-found error later in my code), with no warning from python at any point that the filename was illegal and so was being "cleaned up" for me, behind the scenes. Maybe this is the Windows-like way to do things (longtime mac / linux here).

Playing around a little, I see "open('file  ', 'wb')" also silently changes the filename.
msg201120 - (view) Author: Tim Golden (tim.golden) * (Python committer) Date: 2013-10-24 12:41
I'm going to close this as won't fix: the underlying implementation is simply calling GetFullPathName and removal of trailing spaces doesn't seem too surprising a result. It is possible to manipulate such files using Windows' special \\?\C:\... syntax.

While I admit this could catch you out if you were trying to nail an illegal-but-existent filename, we try not to put warnings in the docs for every cornercase lest they become cluttered.
Date User Action Args
2022-04-11 14:57:46adminsetgithub: 62421
2013-10-24 12:41:44tim.goldensetstatus: open -> closed
resolution: wont fix
stage: resolved
2013-10-24 12:41:31tim.goldensetmessages: + msg201120
2013-07-05 07:31:03ezio.melottisetnosy: + ezio.melotti
2013-06-15 21:00:39Jeremy.Graysetmessages: + msg191233
2013-06-15 14:58:43serhiy.storchakasetnosy: + pitrou, tim.golden, brian.curtin
2013-06-15 12:38:02Jeremy.Graycreate