Thu Aug 12 2021

File Operation

File Name: file-operation.py

#!/usr/bin/evn python

# Open file in write mode
myFile = open('myFile.txt', 'w')
# Write on file
myFile.write('1st test!')
# Close file connection
myFile.close()

# Open file in read mode
myFile = open('myFile.txt', 'r')
# Reading file
data = myFile.read()
print (data)
# Close file connection
myFile.close()

# Open file in append mode
myFile = open('myFile.txt', 'a')
# Write on file
myFile.write('\n2nd test')
# Close file connection
myFile.close()

# Open file in read mode
myFile = open('myFile.txt', 'r')
# Reading file
data = myFile.read()
print (data)
# Close file connection
myFile.close()


#***** Output *****
# 1st test!
# 1st test!
# 2nd test

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.