How can I make a file hidden on Windows?

Question:

On windows you can right-click a file, click on properties and select hidden. How can I do this to a file in python?

Asked By: Jared Parker

||

Answers:

If this is for Windows only:

import win32con, win32api

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

win32api.SetFileAttributes(file,win32con.FILE_ATTRIBUTE_HIDDEN)
Answered By: zipa

If you don’t want/don’t have access to win32 modules you can still call attrib:

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

for a full cross-platform solution see Python cross platform hidden file

this is the easy way

import os
os.system( "attrib +h myFile.txt" )

hide file ‘+h’

show file ‘-h’

myFile.txt can be the full path to your file

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.