Thu Aug 12 2021
File Operation
Python Programming2210 views
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
Reference:
Author:Geekboots