Create a file if it doesn’t exist

For Linux users.

If you don’t need atomicity you can use os module:

import os

if not os.path.exists('/tmp/test'):
    os.mknod('/tmp/test')

macOS and Windows users.

On macOS for using os.mknod() you need root permissions.
On Windows there is no os.mknod() method.

So as an alternative, you may use open() instead of os.mknod()

import os

if not os.path.exists('/tmp/test'):
    with open('/tmp/test', 'w'): pass

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)