xxxxxxxxxx
import os
filename = "myfile.txt" # specify the file name
if not os.path.exists(filename):
with open(filename, 'w') as file:
# perform any desired operations on the file
file.write("This is some sample content.")
print("File created successfully.")
else:
print("File already exists.")
xxxxxxxxxx
import os
filename="file.txt"
answ=os.path.exists(filename)
with open(filename, "a" if answ else "w") as f:
if(answ):
f.write("\n")
f.write("hi!")