[英]Python cross platform hidden file
In *nix I can simply add a .
在* nix我可以简单地添加一个.
to a file to make it "hidden". 到一个文件,使其“隐藏”。 There are also ways to make a file hidden in windows. 还有一些方法可以在Windows中隐藏文件。
Is there a way in python to make a file hidden CROSS PLATFORM? 有没有办法在python中使文件隐藏CROSS PLATFORM?
currently: 目前:
def write_hidden(file_name, data):
file_name = '.' + file_name
with open(file_name_, 'w') as f:
f.write(data)
But as I said, that only works with *nix systems. 但正如我所说,这只适用于* nix系统。
You could do something like this: 你可以这样做:
import os
import ctypes
FILE_ATTRIBUTE_HIDDEN = 0x02
def write_hidden(file_name, data):
"""
Cross platform hidden file writer.
"""
# For *nix add a '.' prefix.
prefix = '.' if os.name != 'nt' else ''
file_name = prefix + file_name
# Write file.
with open(file_name, 'w') as f:
f.write(data)
# For windows set file attribute.
if os.name == 'nt':
ret = ctypes.windll.kernel32.SetFileAttributesW(file_name,
FILE_ATTRIBUTE_HIDDEN)
if not ret: # There was an error.
raise ctypes.WinError()
This has not been tested but should work fine. 这尚未经过测试,但应该可以正常工作。
Also you may wish to see these other questions that helped me implement this: 另外,您可能希望看到帮助我实现此目的的其他问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.