Python tutorial- random access file


random access file


The previous programs write and read data in sequence from the beginning to the end. To write or read data in a position wherever you want is called random access. You will use seek() method to set the file marker position and tell() method to get the current position of the file marker.

 
>>> f=open('Python_source\\test.txt','w')
>>> f.write('DearChanna ')
>>> f.seek(4)
>>> f.write( r ' Mr.Channa')
>>> f.close()
>>> f=open('Python_source\\test.txt','r')
>>> f.read()
'Dear Mr.Channa'
>>> 
Example: build an address book program to manage the information of customers. The program can do the following:
-Append a customer’s information (Name,Email,Tel,Address) to a file.
-Read all customers’ information from the file
-Search a customer by his/her name
-Delete a customer’s information by his/her name

CusMan.py
#write data to file
def writetofile(Name,Email='',Tel='',Address=''):
        try:
                f=open(r'customerlist.txt','a')
                f.write(Name+':'+Email+':'+Tel+':'+Address+'\n')
             
        except Exception:'Print error in writing to file...'
        finally:
                f.flush()
                f.close()

 
#Ask the user to input customer information and call writetofile method to write data to the file
def add():
        Name=raw_input('Name:')
        Email=raw_input('Email:')
        Tel=raw_input('Tel:')
        Address=raw_input('Address:')
        writetofile(Name,Email,Tel,Address)
       
#Get all customers'information
def getall():
        f=open(r'customerlist.txt','r')#open file for reading
        content=f.readlines()#read all lines
       
        f.close()
        return content

 
#Search customer by name      
def search(Name):
        global flag#declare global variable
        try:
                f=open(r'customerlist.txt','r')#open file for reading
                f.seek(0)
                content=f.readline()
               
                while content!='':
                        if content.find(Name)!=-1:
                                print content
                                flag=1
                                return int(f.tell())-int(len(content)+1) #return the position of the matched name
                       
                        else:
                                content=f.readline()
                                flag=0
        except Exception:print 'Error in reading file...'
        finally:
                 f.close()
                 if flag==0:
                         print 'Not found' #Inform the use if the record does not exist
                         return -1 # The record not found-->return -1
       
 #delete customer’s information by name             
def delete(Name):
        print search(Name)
        p=search(Name)
        print "x=",p
        if p!=-1: #Make sure the record exists
               
                st=getall()
                f=open(r'customerlist.txt','w')#open file for writing
                f.writelines(st)
               
                f.seek(p)
                f.write('*****')#write 5 starts to override the 5 characters of the name to be deleted
        else:
                print 'No record to delete'#Inform the use if the record does not exist

 
        f.close()


Comments

Клиентские базы данных!!! Узна comment

 Клиентские базы данных!!! Узна

http://w.w/ Клиентские базы данных!!! Узнайте подробнее Whatsapp: +79139230330 Viber: +79139230330 Telegram: +79139230330 IMO: +79139230330 Email: prodawez@mail.de WWW: http://suo.im/h3Nm2 Телефон: +79139230330
Клиентские базы данных!!! Узнайте подробнее Whatsapp: +79139230330 Viber: +79139230330 Telegram: +79139230330 IMO: +79139230330 Email: prodawez@mail.de WWW: http://suo.im/h3Nm2 Телефон: +79139230330 http://w.w/


2017-10-10



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.