我正在尝试使用 Python 在 Windows 中创建一个隐藏文件,但隐藏后就无法写入了。不过,可以读取此文件。
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
错误:
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'
使用0x22
(即a
属性和h
属性) 而0x02
不是 没用。还是会出现同样的错误。通过 使文件再次可见后,attrib -H test.txt
就可以以写入模式打开它了。不过,据我所知,隐藏文件在 Windows 中应该是可写的。
操作系统:Windows 11
Python版本:Python 3.10.2