Skip to content

open("aux.txt", "w") fails unexpectedly with FileNotFoundError on Windows #81696

Closed

Description

@httpsgithubcomcknoll
BPO 37515
Nosy @pfmoore, @tjguk, @stevendaprano, @zware, @eryksun, @zooba, @https://github.com/cknoll/
Files
  • bug.png
  • 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 2019-07-06.18:11:51.878>
    created_at = <Date 2019-07-06.17:04:53.436>
    labels = ['invalid', 'type-bug', '3.7', 'OS-windows']
    title = '`open("aux.txt", "w")` fails unexpectedly with FileNotFoundError on Windows'
    updated_at = <Date 2019-07-06.23:54:27.495>
    user = 'https://github.com/httpsgithubcomcknoll'

    bugs.python.org fields:

    activity = <Date 2019-07-06.23:54:27.495>
    actor = 'steven.daprano'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-07-06.18:11:51.878>
    closer = 'SilentGhost'
    components = ['Windows']
    creation = <Date 2019-07-06.17:04:53.436>
    creator = 'CarK'
    dependencies = []
    files = ['48459']
    hgrepos = []
    issue_num = 37515
    keywords = []
    message_count = 5.0
    messages = ['347436', '347438', '347439', '347440', '347455']
    nosy_count = 8.0
    nosy_names = ['paul.moore', 'tim.golden', 'steven.daprano', 'SilentGhost', 'zach.ware', 'eryksun', 'steve.dower', 'CarK']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue37515'
    versions = ['Python 3.7']

    Activity

    1. httpsgithubcomcknoll commented on Jul 6, 2019

      httpsgithubcomcknollmannequin
      MannequinAuthor

      I maintain a package which includes a package named "aux.py".
      I could not install it on my windows machine via pip and others had the same problem also with windows.

      I tracked down the problem to io.open. On my Windows 7 System with Python 3.7.1 from Anaconda, the following statements all result in a FileNotFoundError:

      open("aux", "w")
      open("Aux", "w")
      open("AUX", "w")
      open("aux.py", "w")
      open("aux.py", "wb")
      open("aux.txt", "w")

      On the other hand the following commands work as expected:

      open("aaux", "w")
      open("AUX1.txt", "w")
      open("aux2.py", "w")

      etc.

      Can anybody confirm this?

      On Linux (I could not reproduce the problem.)

    2. SilentGhost commented on Jul 6, 2019

      SilentGhostmannequin
      Mannequin

      aux is one of the reserved filenames on windows: https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions

      I don't think Python can do anything about that.

    3. httpsgithubcomcknoll commented on Jul 6, 2019

      httpsgithubcomcknollmannequin
      MannequinAuthor

      This is a good explanation. Indeed Windows complains if I manually want to create a file "aux.txt" ("This device name is not allowed").

      If I want to copy-paste such a file from within a zip-file (Windows Explorer can open zip files) I get an "Unexpected Error".

      I think a descriptive error message would be very helpful here. The best mechanism would be if this was generated on system level and then just passed through by io.open to the generated Exception. But just throwing a "FileNotFoundError" without any hint is potentially frustrating.

      Suggestion for a better Error message: "Could neither open nor create the desired file. Maybe the filename is not allowed by the underlying os".

    4. eryksun commented on Jul 6, 2019

      @eryksun
      Contributor

      DOS device names are reserved in the final component of DOS drive-letter paths. "AUX" (plus an optional colon, spaces, or extension) becomes "\\.\AUX", which is "\??\AUX" in the NT object namespace. By default, "\??\AUX" is a link to "\??\COM1", which, if it exists, is typically a link to "\Device\Serial0". If there's no "COM1", then the kernel returns STATUS_OBJECT_NAME_NOT_FOUND, which the Windows API translates to ERROR_FILE_NOT_FOUND, which the C runtime translates to ENOENT, and finally Python raises FileNotFoundError.

      Accessing a DOS device name requires using a device path (prefixed by "\\.\" or "\\?\", and forward slash is okay, e.g. "//./C:/Temp/aux.py") or a UNC path (e.g. "//localhost/C$/Temp/aux.py"). That said, if we create a file like this, programs that use regular drive-letter paths won't be able to access it. It's better to sanitize reserved DOS device names. This includes "CONIN$" and "CONOUT$", even though Microsoft's documentation overlooks these two.

    5. stevendaprano commented on Jul 6, 2019

      @stevendaprano
      Member

      For future reference, please don't post screen shots of plain text, as they make it unnecessarily difficult for blind or visually impaired developers to contribute (and yes, they do exist, I've worked with some, and at least one core developer). Copy the relevant text (including the exception traceback) and paste them into the bug report.

      Thank you.

    6. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions