Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os.listdir(): inconsistent behavior with trailing spaces #43418

Closed
pronovic mannequin opened this issue May 26, 2006 · 4 comments
Closed

os.listdir(): inconsistent behavior with trailing spaces #43418

pronovic mannequin opened this issue May 26, 2006 · 4 comments

Comments

@pronovic
Copy link
Mannequin

pronovic mannequin commented May 26, 2006

BPO 1495754
Nosy @birkenfeld, @efahl

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2017-06-28.20:59:25.797>
created_at = <Date 2006-05-26.21:28:16.000>
labels = ['OS-windows']
title = 'os.listdir(): inconsistent behavior with trailing spaces'
updated_at = <Date 2017-06-28.20:59:25.797>
user = 'https://bugs.python.org/pronovic'

bugs.python.org fields:

activity = <Date 2017-06-28.20:59:25.797>
actor = 'eric.fahlgren'
assignee = 'none'
closed = True
closed_date = None
closer = None
components = ['Windows']
creation = <Date 2006-05-26.21:28:16.000>
creator = 'pronovic'
dependencies = []
files = []
hgrepos = []
issue_num = 1495754
keywords = []
message_count = 4.0
messages = ['28657', '28658', '28659', '297225']
nosy_count = 4.0
nosy_names = ['georg.brandl', 'pronovic', 'jerrykhan', 'eric.fahlgren']
pr_nums = []
priority = 'normal'
resolution = 'wont fix'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue1495754'
versions = ['Python 2.4']

@pronovic
Copy link
Mannequin Author

pronovic mannequin commented May 26, 2006

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.

@pronovic pronovic mannequin closed this as completed May 26, 2006
@pronovic pronovic mannequin added the OS-windows label May 26, 2006
@pronovic pronovic mannequin closed this as completed May 26, 2006
@pronovic pronovic mannequin added the OS-windows label May 26, 2006
@birkenfeld
Copy link
Member

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.

@jerrykhan
Copy link
Mannequin

jerrykhan mannequin commented Mar 27, 2007

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

@efahl
Copy link
Mannequin

efahl mannequin commented Jun 28, 2017

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

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant