Working with Files
# Open a new file object for writing only
myfile = open("/tmp/newfile.txt", "w")
# Use the write method to add data to the file
myfile.write("Here is my message.\n")
# The "\n" must be added to separate lines on the file
myfile.write("Here is my second message.")
# We must close out the file object in order to properly close the object
myfile.close()with open("/tmp/newfile.txt", "r") as myfile:
for line in myfile:
print(line)
with open('/tmp/cars.txt', 'r') as cars:
print(cars.read())Last updated