windows_support.py 718 B

1234567891011121314151617181920212223242526272829
  1. import platform
  2. def windows_only(func):
  3. if platform.system() != 'Windows':
  4. return lambda *args, **kwargs: None
  5. return func
  6. @windows_only
  7. def hide_file(path):
  8. """
  9. Set the hidden attribute on a file or directory.
  10. From http://stackoverflow.com/questions/19622133/
  11. `path` must be text.
  12. """
  13. import ctypes
  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()