312x Filetype PDF File size 0.27 MB Source: www.wpedu.sch.lk
Python text file handling
Program 1
#open file
fo=open("foo.txt","wb") Output
print ("Name of the file: ", fo.name) Name of the file: foo.txt
print ("Close the file: ", fo.closed) Close the file: False
print ("Openning mode: ", fo.mode) Openning mode: wb
#close file Close the file: True
fo.close()
print ("Close the file: ", fo.closed)
Program 2 Output
#open file for writting file written successfully
fo=open("myfile.txt","w"); Yeah it’s great
#write to file
fo.write ("Python is a great language. \nYeah it’s great");
print ("file written successfully");
fo.close();
Program 3
#read and print a text file
fo=open("myfile.txt","r"); Output
str=fo.read(); Python is a great language.
print(str);
print(fo.tell());
fo.close();
Program 4
#print current working directory
import os Output
print(os.getcwd()); E:\Nayana\pascal prac
Program 5
#read and print a text file
fo=open("myfile.txt","r"); Output
str=fo.read(); Python is a great language.
print(str);
#See the position 27
print(fo.tell());
fo.close();
no reviews yet
Please Login to review.