Python Programming

File Operation

Python code for file operation

8/12/2021
0 views
file-operation.pyPython
#!/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
PythonPython programmingcreate fileread fileappend filefile operation

Loading comments...

Related Examples

Deliver breaking news, insightful commentary, and exclusive reports.

Targeting readers who rely on our platform to stay ahead of the curve.

Contact Us: benzingaheadlines@gmail.com