python check if file exists if not create

f=/tmp/test
if not os.path.exists(os.path.dirname(f)):
os.makedirs(os.path.dirname(f)
with open(f, "a+") as f:
f.write("\ntest")

Leave a Reply

Your email address will not be published. Required fields are marked *