python create file if not exists open and append to file python if exists if not create

The solution for “python create file if not exists open and append to file python if exists if not create” can be found here. The following code will assist you in solving the problem.

import os

if not os.path.exists(path):
with open(path, ‘w’): 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!”)

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in