"""Copyright 2009 Antoine d'Otreppe de Bouvette Visit http://www.aspyct.org for further informations. The subsequent code is available under the terms of the MIT license. This module provides 2 functions: ishidden(path): returns whether the object at "path" is hidden sethidden(path, toggle): if toggle is True, sets the object at "path" as hidden returns the new path to object (it might change under non windows systems) Both of them might raise an OSError if given path is not found. This module requires pywin32 under Windows.""" import sys import os.path if sys.platform in ("win32", "cygwin"): try: from win32file import GetFileAttributes, SetFileAttributes from win32con import FILE_ATTRIBUTE_HIDDEN as HIDDEN def ishidden(path): attrs = GetFileAttributes(path) if attrs == -1: raise OSError("Could not find %s" % path) return HIDDEN & attrs != 0 def sethidden(path, toggle): attrs = GetFileAttributes(path) if attrs == -1: raise OSError("Could not find %s" % path) if toggle: SetFileAttributes(path, attrs | HIDDEN) else: SetFileAttributes(path, attrs ^ HIDDEN) return path except ImportError: print "Cannot import win32file or win32com. Please install pywin32." else: import shutil def ishidden(path): dir, name = os.path.split(path) try: return name[0] == "." except IndexError: # Should not happen, but we never know... return False def sethidden(path, toggle): dir, name = os.path.split(path) try: hidden = name[0] == "." except IndexError: # Same as above hidden = False if toggle: if not hidden: newpath = os.path.join(dir, ".%s" % name) shutil.move(path, newpath) else: if hidden: newpath = os.path.join(dir, name[1:]) shutil.move(path, newpath) return newpath