How to make this FILE process in Python?
The Standard Input, Standard Output are the two fileobjects genrally accessible from the operating system provided interfaces which allows us to change files.
How to redirect this output... normally output is coming from print command in the Python. Can you send the data/text to a file? Yes we can using file object.
The Standard Input, Standard Output are the two fileobjects genrally accessible from the operating system provided interfaces which allows us to change files.
How to redirect this output... normally output is coming from print command in the Python. Can you send the data/text to a file? Yes we can using file object.
File IO Process in Python |
The file open modes
Python file open modes are same as C language by default it will be open in read mode. The open() method returns a file object or file pointing reference.
Syntax:
fileobject = open(file_name [, access_mode][, buffering])
where basic file operational mode can be 'r' (read), 'w'(write) or 'a'(append).
The default mode is 'r' that is read mode and the file can be ANSI Text file or binary file (b). They can be opened in combined mode ‘rb+’
That every file that you opened in the code must be closed. The close() method closes an opened file object or you can dereferences.
Syntax: fileObject.close()
File Advanced open modes
File Advanced opening modes in Python |
The file attributes
Following table have the list of the file object attributes. Which we can directly access them public.Attribute | Description |
---|---|
file.closed | Returns true if file is closed, false otherwise. |
file.mode | Returns access mode with which file was opened. |
file.name | Returns name of the file. |
# This program illustrates the file attributes f=open('fileattr.py') print "mode:",f.mode print "name:",f.name if f.closed: print f.name, " is closed" else: print f.name," is not closed"Its execution gives the output as follows
>>> execfile('fileattr.py') mode: r name: fileattr.pyfileattr.py is not closed
We started experimenting with Files and Pickle soon we will update you on that... Object oriented pickling !!!
- Create your own object
- Push the object into pickle
- Dump, load functions explore
- persistance of objects
- Reusing the object after a restart of Python Shell
Pickle for Patient object persistance
import pickle """ This program is to illustrate the Pickle module usage in Python """ class Patient: def __init__(self, n, a): self.name=n; self.age=a def __repr__(self): return self def Printpatient(self): print self.name, self.age if __name__=="__main__": """ This is main program for Patient database program """ p=[] # This list is to dump x=[] # This list is for load from pickle for i in range(0,2): n=raw_input('Enter name of the Patient:' ) a=input('Enter age of the patiet: ') p.append(Patient(n,a)) #Storing the list of Patient objects into a file fp=open('Patient.txt','wb') # Write in bin format pickle.dump(p,fp) fp.close() fp=open('Patient.txt','r+') x=pickle.load(fp) for i in x: i.Printpatient()
>>> execfile('c:/pybin/PatientFile.py') Enter name of the Patient:Nageshwarrao Enter age of the patiet: 89 Enter name of the Patient:Sridhar Enter age of the patiet: 51 Nageshwarrao 89 Sridhar 51