关于python:os.mknod()在MacOS上失败了?

os.mknod() fails on MacOS?

os.mknod()是Mac上的特权呼叫吗?它总是失败,不允许操作?

1
2
3
4
5
6
7
8
9
In [1]: import os

In [2]: os.mknod("/tmp/test123")
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-2-1b8032a076af> in <module>()
----> 1 os.mknod("/tmp/test123")

OSError: [Errno 1] Operation not permitted


从OSX手册页https://developer.apple.com/library/mac/documentation/darwin/reference/manpages/man2/mknod.2.html

1
Mknod() requires super-user privileges.

除无效参数外,其他参数都有效

1
sudo python -c"import os; os.mknod('/tmp/test123')"

如@helloworld所示,MacOS需要超级用户权限才能运行mknod。如果您不想升级权限来创建文件,可以改为这样做:

1
open('/tmp/test123', 'w').close()

如果您想立即向文件中写入内容,我建议您这样做:

1
2
3
with open('/tmp/test123', 'w') as file:
    file.write('hello world')
# Once the above block is exited the file will be automatically closed for you