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.listdir(): inconsistent behavior with trailing spaces
Type: Stage:
Components: Windows Versions: Python 2.4
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: eric.fahlgren, georg.brandl, jerrykhan, pronovic
Priority: normal Keywords:

Created on 2006-05-26 21:28 by pronovic, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg28657 - (view) Author: Kenneth J. Pronovici (pronovic) Date: 2006-05-26 21:28
I've noticed some inconsistent behavior around 
os.listdir() and paths that contain trailing spaces on 
the Windows platform.

Take this example code:

   import os
   import tempfile
   tmpdir = tempfile.mkdtemp()
   newdir = os.path.join(tmpdir, " collect dir ")
   os.mkdir(newdir)
   print os.path.exists(newdir)
   print os.listdir(newdir)

If I run this code on Windows 2000 Pro using Python 
2.4.2, I get this:

True
[Errno 3] The system cannot find the path 
specified: 'c:\\docume~1\\a0clu0~1.bcb\\locals~1
\\temp\\tmpfd7j3t\\ collect dir /*.*'

It seems to me that if os.path.exists() tells me that 
a file or directory exists, that os.listdir() should 
be able to operate on it.
msg28658 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-05-29 20:59
Logged In: YES 
user_id=849994

Since Python only calls the underlying Win32 function
FindFirstFile, I think there can nothing be done since that
function seems to be buggy in this respect.
msg28659 - (view) Author: JerryKhan (jerrykhan) Date: 2007-03-27 11:41
I agree with the fact that this inconsistent behaviour is disturbing.
I tried it with a simple example "lib ", if it exists, we must be able to browse it.

Example: open the idle, 
import os
os.path.exists("lib ")   ==> True
os.path.listdir('lib ')

Traceback (most recent call last):
  File "<pyshell#36>", line 1, in -toplevel-
    os.listdir('lib ')
WindowsError: [Errno 3] Le chemin d'accès spécifié est introuvable: 'lib /*.*'

os.stat('lib ') works ...
os.stat(' lib') does not work (that ok to me) 

I disagree with the argument that since Windows is bugged then Python is bugged too.
It should be possible to rightstrip the folder name before asking for the resolution of
 abspath/the_dir_with_space_at_the_end /*.*
Or to deactivate the rightstriping done on the stat builtin function.

I imagine that this is located in nt builtin module.
I have done it in my own program .. but this should be coded in interfaces modules.

Jerrykhan
msg297225 - (view) Author: Eric Fahlgren (eric.fahlgren) * Date: 2017-06-28 20:59
Would it be appropriate to make a comment about this Windows bug in both os.stat and os.path.exists documentation?  I just stumbled upon it independently (both Win7 and 10, both Py 2.7 and 3.6) with nearly the same incantation that Kenneth reported over 10 years ago (exists check success, followed by scandir failure).

[My workaround was to append os.sep to the directory spec:
  os.path.exists(newdir+os.sep) -> False
]

Clearly this behavior is not going away, so it should at least be mentioned somewhere more prominently than on a decade old BPO item...
History
Date User Action Args
2022-04-11 14:56:17adminsetgithub: 43418
2017-06-28 20:59:25eric.fahlgrensetnosy: + eric.fahlgren
messages: + msg297225
2006-05-26 21:28:16pronoviccreate