[Python] Making file hidden in Windows using Python blocks file writing

Discussão em 'Python' iniciado por Stack, Abril 16, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to make a hidden file in Windows using Python, but after I make it hidden it becomes impossible to write. However, reading this file is possible.

    import ctypes

    with open("test.txt", "r") as f: print('test')

    ctypes.windll.kernel32.SetFileAttributesW("test.txt", 0x02) # adding h (hidden) attribute to a file

    with open("test.txt", "r") as f: print('test') # works
    with open("test.txt", "w") as f: print('test') # error


    Error:

    Traceback (most recent call last):
    File "C:\Users\user\Documents\client\test.py", line 8, in <module>
    with open("test.txt", "w") as f: print('test') # error
    PermissionError: [Errno 13] Permission denied: 'test.txt'


    Using 0x22 (which is a attribute and h attribute) instead of 0x02 doesnt't help. The same error occurs. After making the file visible again via attrib -H test.txt it becomes possible to open it in write mode. However, as far as I know, hidden files should be writable in Windows.

    OS: Windows 11

    Python version: Python 3.10.2

    Continue reading...

Compartilhe esta Página