Python Working With Files
Working With Files
Your Programs can read fram files and write to files. Files are opened in read mode ('r') by default, but can also be opened in write mode ('w') and append mode ('a').
python_file.py
#Reading a file and storing its lines filename = 'journal.txt' with open(filename) as file_object: lines = file_object.readlines() for line in lines: print(line) #Writing to a file filename = 'journal.txt' with open(filename, 'w') as file_object: file_object.write("I love programming.") #Appending to a file filename = 'journal.txt' with open (filename, 'w') as file_object: file_object.write("\nI love making games.")
No comments:
Post a Comment