Write file with specific permissions in Python

前端 未结 6 1225
一个人的身影
一个人的身影 2020-12-01 15:37

I\'m trying to create a file that is only user-readable and -writable (0600).

Is the only way to do so by using os.open() as follows?

6条回答
  •  甜味超标
    2020-12-01 16:23

    What's the problem? file.close() will close the file even though it was open with os.open().

    with os.fdopen(os.open('/path/to/file', os.O_WRONLY | os.O_CREAT, 0o600), 'w') as handle:
      handle.write(...)
    

提交回复
热议问题