First you have a syntax error because os.system()
expects a string argument and you have not provided one. This will fix your code:
os.system('touch {}'.format(gdsfile))
which constructs a string and passes it to os.system()
.
But a better way (in Python >= 3.3) is to simply open the file using the open()
builtin:
gdsfile = "/home/hha/temp.gds"
try:
open(gdsfile, 'x')
except FileExistsError:
pass
This specifies mode x
which means exclusive creation - the operation will fail if the file already exists, but create it if it does not.
This is safer than using os.path.isfile()
followed by open()
because it avoids the potential race condition where the file might be created by another process in between the check and the create.
If you are using Python 2, or an earlier version of Python 3, you can use os.open()
to open the file with the exclusive flag.
import os
import errno
gdsfile = "/home/hha/temp.gds"
try:
os.close(os.open(gdsfile, os.O_CREAT|os.O_EXCL))
except OSError as exc:
if exc.errno != errno.EEXIST:
raise