2017-04-16 52 views
2

在windows上,您可以右鍵單擊文件,單擊屬性並選擇隱藏。我如何做到這一點在Python中的文件?如何在Windows上隱藏文件?

+4

[Python中的可能的複製跨平臺隱藏文件](http://stackoverflow.com/questions/25432139/python-cross-platform-hidden-file) – lmiguelvargasf

+1

根據[this]使用'cmd'(http://stackoverflow.com/questions/5486725/how-to-execute-a-command-prompt-command-from-python)問題並嘗試'attrib + h PathToFile' – Xaqron

+0

@ Xaqron,它是'attrib.exe'。它不是cmd shell的內置命令。 – eryksun

回答

3

如果你不想/不能訪問Win32的模塊,你可以還叫attrib

import subprocess 
subprocess.check_call(["attrib","+H","myfile.txt"]) 
+0

@eryksun謝謝。之後我試圖恢復隱藏的旗幟時出現在我的測試中。固定。 –

1

如果這是僅適用於Windows:

import win32con, win32api 

file = 'myfile.txt' #or full path if not in same directory 

win32api.SetFileAttributes(file,win32con.FILE_ATTRIBUTE_HIDDEN) 
+0

您正在替換現有的文件屬性。它必須從'GetFileAttributes'中按位ORd到現有屬性中。 – eryksun