datafile=open("myfile.dat","w") #open a file for writing
datafile.write("Sri Lanka,India,Pakistan,Bhutan,Australlia,Canada") #write data
datafile.write("\n")
datafile.write("Apple,Orange,Mango")
datafile.close() #release the file
#reading from the file
datafile=open("myfile.dat","r") #open file for reading
record=datafile.readline()#reding a line (here it is the first line)
mylist=record.strip("\n").split(",") #remove newline character and split the string by comma
print(mylist)
print(len(mylist))
Showing posts with label open. Show all posts
Showing posts with label open. Show all posts
Friday, January 20, 2012
Python Note 5 (File Handling)
File handling is very easy in python, Lets see an example
Labels:
file handling,
open,
Python
Subscribe to:
Posts (Atom)