Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

os.mknod() fails on MacOS?

Tags:

python

Is os.mknod() a privileged call on Mac? It always fails with operation not permitted?

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
like image 954
python152 Avatar asked Aug 20 '15 10:08

python152


2 Answers

From the OSX manpage https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/mknod.2.html

Mknod() requires super-user privileges.

Works except from the invalid argument

sudo python -c "import os; os.mknod('/tmp/test123')"
like image 146
HelloWorld Avatar answered Sep 20 '22 13:09

HelloWorld


Unfortunately mknod requires escalated privileges. If you don't need mknod specifically though, just create the file with open, which doesn't require escalation:

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

If you want to write to the file in addition to creating it:

with open('/tmp/test123', 'w') as file:
    file.write('hello world')

Using with as above will automatically close the file for you.

like image 43
Cory Klein Avatar answered Sep 18 '22 13:09

Cory Klein