这个问题与答案有关在Python中写入具有特定权限的文件 https://stackoverflow.com/questions/5624359/write-file-with-specific-permissions-in-python用于打开具有特定权限的文件进行写入(用Python)。
答案中的代码如下所示:
with os.fdopen(os.open('foo', os.O_APPEND | os.O_CREAT, 0o644)) as out:
out.write("hello\n")
2.7.1 中的这段代码(我的公司没有安装 2.7.3)产生:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
IOError: File not open for writing
os.fdopen
有自己的模式参数,但设置没有帮助:
>>> with os.fdopen(os.open('foo', os.O_APPEND | os.O_CREAT, 0o644), 'a') as out:
... out.write("hello\n")
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument
长话短说,我无法弄清楚如何实际写入通过以下方式打开的文件os.fdopen
and os.open
。有任何想法吗? 2.7.1 中的已知错误?
提前致谢!