Home > Article > Backend Development > Getting Started with Python Reading and Writing Files
1. Open the file and read all the contents
file_object = open('thefile.txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )
2. Read Fixed bytes
file_object = open('abinfile', 'rb')
try:
while True:
chunk = file_object.read(100)
if not chunk:
break
do_something_with(chunk)
finally:
file_object .close( )
3. Read one line of the file
f = open("D:\test\BlueSoftSetup.log","r")
try:
while True:
line = f. Readline () l if line:
Print (line)
else:
break;
finally:
f.close ();
4. output = open('data', 'w')
Write binary fileoutput = open('data', 'wb')
Append write file
output = open('data', 'w+')
Write data
file_object = open('thefile.txt', 'w')
file_object.close( )