0

Under Linux I can create files in Python with os.open and a specific permission this way:

open_flags = os.O_RDWR | os.O_CREAT | os.O_TRUNC
open_mode = 0o660
temp_umask = 0o002
original_umask = os.umask(temp_umask)  # Sets the umask and stores the old umask
fd = os.open(self._lock_file, open_flags, open_mode)

This way I get a file with 0o660 - rw-rw----.

This doesn't work for Windows:

fd = os.open("foobar",(os.O_RDWR|os.O_CREAT|os.O_TRUNC),0o660)
print(f'{os.stat("foobar")}')
print(f'{oct(S_IMODE(os.stat("foobar").st_mode))}')

This creates a file with 0o666. I know that file permissions under windows work very different from Linux and that there is no such thing as a umask.

So, my question would be: How can I create with os.open a file which is non executable and has the lowest possible rights for just read + write? How can the octal permission be interpreted for Windows?

martineau
  • 112,593
  • 23
  • 157
  • 280
  • Windows uses access control lists instead of control modes. The short answer is: no, this is not easy to implement in Windows. Here are two answers that may help: https://stackoverflow.com/questions/27500067/chmod-issue-to-change-file-permission-using-python and https://stackoverflow.com/questions/34698927/python-get-windows-folder-acl-permissions – James Oct 30 '21 at 21:49

0 Answers0