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.isdir returns true for dots
Type: behavior Stage: resolved
Components: Windows Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, morha13, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2017-10-06 15:26 by morha13, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg303827 - (view) Author: Mor Haviv (morha13) Date: 2017-10-06 15:26
I uploaded this as a question on Stack Overflow and I suspect it might be a bug. Here is the link for the Stack Overflow question: https://stackoverflow.com/questions/46608731/python-os-path-isdir-returns-true-for-dots/46608842#46608842

The problem itself (copied from what I uploaded on Stack Overflow):

I'm programming my own shell in python. Right now I'm trying to implement the `cd` command to my shell.

The function that performs this command has several variables:

`self.current_dir = "C:\\"` - The default value, it changes depends on the user's input using the cd command

`dir = "..."` - The requested directory that the user types. "..." is an example for an input that causes the problem.

Here is my code:

    def command_cd(self, dir):
        if os.path.isdir(self.shell.current_dir + dir):
            self.shell.current_dir = self.shell.current_dir + dir + "\\"

The problem is that for some strange reason, `os.path.isdir(self.shell.current_dir + dir)` returns `True` when the user types dots (Just like the example inputs for the variables which I gave above). 

The problem occurs even if you change the amount of dots (even above 5 dots) and I really have no idea what causes it.

There's obviously no folder named `...` or anything like it.


**If my problem isn't clear enough please comment and I'll edit it**
msg303832 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2017-10-06 15:55
This is standard Windows API behavior for the final path component. A single dot component means the current directory. Two dots means the parent directory. More than two dots and/or trailing spaces, gets reduced to a single dot, meaning the current directory. For example:

    >>> os.path.abspath('.')
    'C:\\Temp'
    >>> os.path.abspath('..')
    'C:\\'
    >>> os.path.abspath('...')
    'C:\\Temp'
    >>> os.path.abspath('... ... ...')
    'C:\\Temp'

Specifically, os.path.isdir is implemented as nt._isdir, which calls WinAPI GetFileAttributes to check for FILE_ATTRIBUTE_DIRECTORY, which in turn calls the NT system function NtQueryAttributesFile. 

GetFileAttributes has to translate the DOS path to an NT kernel path. In the kernel, none of this "." business exists. The kernel doesn't even have a concept of a working directory. Depending on your Windows version, it might call the runtime library function RtlDosPathNameToNtPathName_U_WithStatus to convert the path to a native OBJECT_ATTRIBUTES record. The first step is to normalize the path via RtlGetFullPathName_Ustr, which is what the Windows API GetFullPathName function calls like in the above abspath() examples.
History
Date User Action Args
2022-04-11 14:58:53adminsetgithub: 75897
2017-10-06 15:55:11eryksunsetnosy: + eryksun
messages: + msg303832
2017-10-06 15:43:19morha13setresolution: not a bug
2017-10-06 15:42:58morha13setstatus: open -> closed
stage: resolved
2017-10-06 15:26:29morha13create