windows_support.py 714 B

123456789101112131415161718192021222324252627282930
  1. import platform
  2. import ctypes
  3. def windows_only(func):
  4. if platform.system() != 'Windows':
  5. return lambda *args, **kwargs: None
  6. return func
  7. @windows_only
  8. def hide_file(path):
  9. """
  10. Set the hidden attribute on a file or directory.
  11. From http://stackoverflow.com/questions/19622133/
  12. `path` must be text.
  13. """
  14. __import__('ctypes.wintypes')
  15. SetFileAttributes = ctypes.windll.kernel32.SetFileAttributesW
  16. SetFileAttributes.argtypes = ctypes.wintypes.LPWSTR, ctypes.wintypes.DWORD
  17. SetFileAttributes.restype = ctypes.wintypes.BOOL
  18. FILE_ATTRIBUTE_HIDDEN = 0x02
  19. ret = SetFileAttributes(path, FILE_ATTRIBUTE_HIDDEN)
  20. if not ret:
  21. raise ctypes.WinError()