Showing posts with label file handling. Show all posts
Showing posts with label file handling. Show all posts

Friday, January 20, 2012

Python Note 5 (File Handling)

File handling is very easy in python, Lets see an example
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))

© kani.stack.notez 2012 | Blogger Template by Enny Law - Ngetik Dot Com - Nulis