File Operation

#!/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

Recommended for you