Python Working With Files - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript Python Working With Files - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Saturday, December 1, 2018

Python Working With Files

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.")


Screenshot Example 


python file handling

journal.txt 


python file handling

No comments:

Post a Comment

Post Top Ad