file = open(“testfile.txt”,”w”)
file.write(“Hello World”)
file.write(“This is our new text file”)
file.write(“and this is another line.”)
file.write(“Why? Because we can.”)
file.close()
python write to file while reading
# writin content of one file to the other
with open("source.txt", "r") as inFile, open("target.txt", "w") as outFile:
for line in inFile:
print(line.strip("\n"), file=outFile)