Datafile Handeling Presentation by UNKNOWN pptx

aitools59 21 views 48 slides Oct 12, 2024
Slide 1
Slide 1 of 48
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48

About This Presentation

Computer Science Data file Handeling project


Slide Content

DATA FILE HANDLING A file is a sequence of characters / data which occupies named place on the disk where a sequence of related data is stored. Data file operation Opening a file. Performing operations(read, write) Closing the file . Three major operation we have to perform on data file : reading, writing and appending. Three type of data file supported : TEXT FILE & BINARY FILES & CSV FILES. TEXT FILE BINARY FILE A text file store information in ASCII or Unicode characters. In text files each line of text is terminated with a special character – EOL(end of line). A binary file is just a file contain information in the same format in which the information is held in the memory.. In binary file there is no delimiter for line.

OPENING & CLOSING THE DATA FILE : Open() function is used to open a data file. It takes two arguments Name if the file Mode of the file. Syntax : File object = open(filename , access mode) Eg - abc =open(“ check.txt”,”r ”) FILE OBJECT FILE NAME FILE MODE Always create the file in the same folder where your python has been installed…. We can skip file mode from the open(0 as it is an optional parameter , then file is opened in read mode by default Close() function is used to flushes any unwritten information and closes the file object , after which no more writing can be done. Syntax - file_object.close ()

FILE MODES

VARIOUS METHODS RELATED TO THE FILE OBJECT Name : name of the file opened Mode : mode in which file is opened Closed : returns Boolean value (true/false) which indicates the file is closed or not. Readable : returns Boolean value which indicate file is readable or not. abc = open("check.txt") print("file name is ",abc.name) print("file mode ", abc.mode ) print("file is readable ", abc.readable ()) print("file closed ", abc.closed ) abc.close () print("file closed ", abc.closed )

abc = open("check.txt") # pr= abc.read () #print("Reading and Printining entire data file - check.txt") #print(pr) r = abc.read (15) print("printing first 15 characters : ",r) abc.close () READING DATA FROM THE FILE read() Reads at most n bytes from the file, if n is not given then reads the entire file. readline () Reads a line of input, if n is specified reads at most n bytes. readlines () Read all the lines and return them in a list NOTEPAD FILE : CHECK.TXT

abc = open("check.txt") pr= abc.readline () print("printing first line of the text file :- “) Print( pr) abc.close () abc = open("check.txt") pr= abc.readlines () for i in pr: print( i,end ="") abc.close () Example of readline () Example of readlines () First call of the function will return the first line and so on Line is terminated by \n. abc = open("check.txt") for i in abc : print( i,end ="") abc.close () Another way to read entire data from the file NOTEPAD FILE : CHECK.TXT

abc = open(" english.txt",' w ') abc.write ("describing data file handling concepts \n") abc.close () WRITING DATA TO FILE file mode ‘w’ write() Write string to the file. We have to add \n char. To store data in separated lines. writelines () Write function can not be used to write list, tuple etc. sequence of data including string can be written using writeline () NOTEPAD FILE : english.txt abc = open(" english.txt", 'a ' ) abc.write ("this topic is for class xii \n") abc.close () NOTEPAD FILE : english.txt Appending data to the text file using file mode – ‘a’

EXAMPLE OF writelines () abc = open(" english.txt",'w ') s="this is my book of cs. \ nthis is for class xii" abc.writelines (s) abc.close () NOTEPAD FILE : english.txt abc = open(" book.txt",'w ') l=[' cs \ n','java \ n','python \ n','mysql \n'] abc.writelines (l) abc.close () Writing a list in a text file NOTEPAD FILE : book.txt

WAP TO OBTAIN ROLL NO, NAME AND MARKS OF THREE STUDENTS AND THEN STORE IS DATA IN A TEXT FILE. xyz=open(" student_data.txt","w ") for i in range(1,4): rno = int (input("enter rollno ")) name = input("enter name ") marks = int (input("enter marks ")) xyz.write ( str ( rno )) xyz.write (name) xyz.write ( str (marks)) xyz.write ("\n") xyz.close () INPUT TEXT FILE xyz=open(" student_data.txt","r ") pr = xyz.readlines () for i in pr: print( i,end ="") xyz.close () PROGRAM TO READ THE CONTENT OF TEXT FILE

NOTEPAD FILE : CHECK.TXT WAP TO COUNT AND DISPLAY THE NUMBER OF CAPITAL VOWELS IN A FILE CHECK.TXT xyz=open(" check.txt","r ") pr = xyz.read () c=0 for i in pr: if i in "AEIOU": c=c+1 print("NUMBER OF CAPITAL VOWELS IN THE TEXT FILE ",c) xyz.close ()

NOTEPAD FILE : COMPUTER.TXT WAP TO COUNT AND DISPLAY THE NUMBER OF SPACES IN A TEXT FILE COMPUTER.TXT xyz=open(" computer.txt","r ") pr = xyz.read () c=0 for i in pr: if i ==' ': c=c+1 print("NUMBER OF SPAES IN TEXT FILE ",c) xyz.close ()

NOTEPAD FILE : COMPUTER.TXT WAP TO COUNT AND DISPLAY THE NUMBER OF SPECIAL CHARCTER IN A TEXT FILE COMPUTER.TXT xyz=open(" computer.txt","r ") pr = xyz.read () c=0 for i in pr: if i.isalnum ()== False: c=c+1 print("NUMBER OF SPECIAL CHARACTER IN TEXT FILE - ",c) xyz.close ()

WAP TO COUNT THE NUMBER OF WORDS IN A TEXT FILE COMPUTER.TXT (2 WAYS OF DOING SAME PROGRAM) NOTEPAD FILE : COMPUTER.TXT xyz=open(" computer.txt","r ") pr = xyz.read () k= pr.split () c=0 print(k) for i in k: c=c+1 print("no of words ",c) xyz.close () xyz=open(" computer.txt","r ") pr = xyz.read () c=0 for i in pr: if i ==' ': c=c+1 print("no of words ",c+1) xyz.close ()

NOTEPAD FILE : CHECK.TXT WAP TO DISPLAY AND COUNT THE NUMBER OF WORDS IN A TEXT FILE WHICH ARE STARTING FROM “T” xyz=open(" check.txt","r ") pr = xyz.read () c=0 w= pr.split () for i in w: if i [0]=="T": print( i ) c=c+1 print(c) xyz.close () WAF TO DISPLAY AND RETURN THE NUMBER OF WORDS IN A TEXT FILE WHICH ARE STARTING FROM “T” def fun1(): xyz=open(" check.txt","r ") pr = xyz.read () c=0 w= pr.split () for i in w: if i [0]=="t": print( i ) c=c+1 xyz.close () return c R=Fun1() Print( r)

NOTEPAD FILE : CHECK.TXT WAP TO COPY DATA FROM FILE “CHECK.TXT” TO “CHECK1.TXT” AFTER CONVERTING THE CHARACTERS TO TOGGLE CASE. xyz=open(" check.txt","r ") abc =open("check1.txt","w") pr = xyz.read () for i in pr: if i.isupper (): i = i.lower () elif i.islower (): i = i.upper () print( i,end ="") abc.write ( i ) xyz.close () abc.close () NOTEPAD FILE : CHECK1.TXT

NOTEPAD FILE : CHECK.TXT WAP TO DISPLAY AND COUNT THE NUMBER OF LINES IN A TEXT FILE WHICH ARE STARTING FROM “P” xyz=open(" check.txt","r ") pr = xyz.readlines () c=0 for i in pr: if i [0]=="P": print( i ) c=c+1 print("number of line starting from P ",c) xyz.close ()

WAP TO OTAIN CHARACTERS FROM THE USER TILL USER ENTER “-1” AND STORE ALL CAPIATL CHARCTERS TO “UPPER.TXT”, SMALL LETTERS TO “LOWER.TXT”, DIGITS TO “DIGITS.TXT” AND OTHER CHARACTERS TO “OTHERS.TXT” up=open(" upper.txt","w ") lw =open(" lower.txt","w ") ot =open(" others.txt","w ") dg=open(" digits.txt","w ") while True: s=input("enter a character ") if s== "-1": break if s.isupper (): up.write (s) elif s.islower (): lw.write (s) elif s.isdigit (): dg.write (s) else: ot.write (s) lw.close () dg.close () ot.close () up.close ()

xyz=open(" check.txt","r ") pr = xyz.readlines () print("last line of the file ") print(pr[-1]) xyz.close () WAP TO DISPLAY THE LAST LINE OF THE TEXT FILE CHECK.TXT NOTEPAD FILE : CHECK.TXT

''' waf to count number of me and my in a text file''' def fun1(): abc =open("check.txt") f= abc.read () m= f.split () c=0 for i in m: if i =="me" or i =="my": c=c+1 print("total me and my ",c) abc.close () fun1()

abc =open(" hash.txt","r ") pr= abc.readlines () for i in pr: l= len ( i ) for j in range(l): if i [j]=="#": print( i ) abc.close () WAP TO DISPLAY ALL THE LINES FROM THE FILE WHICH CONTAIN PYTHON COMMENT CHARACTER “#” NOTEPAD FILE : HASH.TXT

abc =open(" check.txt","r ") pr= abc.read () w= pr.split () for i in w: if i [-1]=="m": print( i ) abc.close () WAP TO DISPLAY ALL THE WORDS ENDING WITH “M” IN THE TEXT FILE “CHECK.TXT” NOTEPAD FILE : CHECK.TXT

abc =open(" check.txt","r ") pr= abc.read () w= pr.split () c=0 for i in w: if len ( i )<4: print( i ) c=c+1 print("total number of words ",c) abc.close () WAP TO DISPLAY ALL THE WORDS HAVING LENGTH LESS THAN 4 ALSO COUNT THESE WORDS. NOTEPAD FILE : CHECK.TXT

WAP to obtain 2 numbers from the user and then display the square Of the maximum number and also store all the three numbers in a text file Max_num.txt. Also write another program to read and display the content of max_num.txt file. n= int(input(“num1 “)) m=int(input(“num2 “)) if n>m: s=n*n else: s=m*m abc =open(“ max_num.txt”,”w ”) abc.write (str(n)) abc.write (str(m)) abc.write (str(s)) abc.close () xyz =open(“ max_num.txt","r ") pr = xyz.readlines () for i in pr: print( i ) xyz.close ()

CREATING A BINARY FILE : ADDING RECORDS IN A BINARY FILE USING A LIST import pickle st =[] ch ='y' while ch =='y': rno = int (input("enter roll num ")) name=input("enter name ") marks = int (input("enter marks ")) s=[ rno,name,marks ] st.append (s) ch =input("want to add more ") f=open(" st_data.dat","wb ") pickle.dump ( st,f ) f.close () READING DATA FROM THE BINARY FILE : AS A LIST import pickle f=open(" st_data.dat","rb +") stu = pickle.load (f) for i in stu : print( i ) f.close () File operations ? Creation / adding a record Display all rec Searching Modify / update Delete PROJECT

import pickle f=open("st_data.dat "," rb +") stu = pickle.load (f) n= int (input("enter roll number to be update ")) for i in stu : s= i [0] if s==n: print("record found ") i [1]=input("enter name ") i [2]= int (input("enter marks ")) print("record update") break else: print(" rec not found") f.seek (0) “will move the file pointer to the first record” pickle.dump ( stu,f ) f.close () import pickle f=open(" st_data.dat","rb ") stu = pickle.load (f) r= int (input("roll numbe ")) for i in stu : if i [0]==r: print(" rec found") print( i [1],":", i [2]) break else: print(" rec not found") f.close () SEARCHING DATA IN A BINARY FILE UPDATING DATA IN A BINARY FILE

import pickle f=open(" maps.dat","ab ") ch ="y" while ch == "y": r= int (input("input enter rno ")) s=input("enter name ") m= int (input("input enter marks ")) l2={" rno ": r,"name ": s,"MARKS ":m} pickle.dump (l2,f) ch =input("want to enter more rec ") f.close () CREATING A BINARY FILE : ADDING RECORDS IN A BINARY FILE USING A DICTIONARY import pickle f=open(" maps.dat","rb ") d={} try: while True: d= pickle.load (f) print(d) except EOFError : f.close () READING DATA FROM THE BINARY FILE : AS A DICTIONARY

import pickle f=open(" st_data.dat","rb +") d=[] stu = pickle.load (f) n= int (input("enter roll number to be delete ")) for i in stu : s= i [0] if s!=n: print("record found ") d1=[ i [0], i [1], i [2]] d.append (d1) print("record deleted") else: print(" rec not found") f.seek (0) pickle.dump ( d,f ) f.close () Deletion logic Copy the records other than the Record to be deleted in a list and then dump this List in a file File1.dat list 1 1 2 2 3 this is to left out 4 4 5 5 Rec = 3 to be deleted DELETION IN A BINARY FILE

WRITING DATA TO CSV FILE READING DATA FROM CSV FILE

Ques1-Write a definition for function BUMPER() in PYTHON to read each object of a binary file GIFTS.DAT, find and display details of those gifts, which have remarks as “ON DISCOUNT”. Assume that the file GIFTS.DAT is created with the help of lists of following type: (ID, Gift, Remarks, Price) def bumper(): import pickle f=open(" gifts.dat","rb ") cont= pickle.load (f) c=0 print("details of gifts having remarks as - on discount") for i in cont: if i [2]=="on discount": c=1 print( i ) f.close () if c==0: print(“rec not found”) bumper()

Ques2-Following is the structure of each record in a data file named ”PRODUCT.DAT”. [CODE, DESC,VALUE] The values for prod_code and prod_desc are strings, and the value for stock is an integer. Write a function in PYTHON to update the file with a new value of stock. The product_code , whose DESC AND STOCK VALUE is to be updated, is to be input during the execution of the function import pickle st =[] ch ="y" while ch =="y": pcode =input("enter product code=") desc =input("enter desc of product=") value= int (input("enter value=")) s=[ pcode,desc,value ] st.append (s) ch =input("do you want to cont(y/n)=") f=open(" product.dat","wb ") pickle.dump ( st,f ) f.close () def fun( n,d,s ): import pickle f=open(" product.dat","rb +") cont= pickle.load (f) for i in cont: if i [0]==n: i [1]=d i [2]=s f.seek (0) pickle.dump ( cont,f ) f.close () a=input("enter product code to be updated") desc =input("enter new desc =") stock= int (input("enter new stock value=")) fun( a,desc,stock )

Write a function in PYTHON to search for a laptop from a binary file “LAPTOP.DAT” containing the records of following type. The user should enter the model number and the function should display the details of the laptop. [ ModelNo , RAM, HDD, Details] where ModelNo , RAM, HDD are integers, and Details is a string. def search(m): file=open(' LAPTOP.dat','rb +') data= pickle.load (file) for i in data: if i [0]==m: print('record found!') print('MODEL NO-', i [0]) print('RAM-', i [1]) print('HDD-', i [2]) print('DETAILS-', i [3]) break if i [0]!=m: print('record not found!') file.close () m= int (input('enter the model no. of the laptop-')) search(m) import pickle file = open ( ' LAPTOP.dat' , 'wb ' ) ch = 'y' l=[] while ch == 'y' : model_no = int ( input ( 'enter the model no-' )) ram= int ( input ( 'enter the ram-' )) hdd = int ( input ( 'enter the hdd -' )) details= input ( 'enter the details-' ) s=[ model_no,ram,hdd,details ] l.append (s) print ( 'record added!' ) ch = input ( 'do you want to continue(y/n)-' ) pickle.dump ( l, file ) file .close ()

REVISION OF TEXT FILE : WAP TO READ AND DISPLAY THOSE LINE WHICH STARTS WITH ALPHABET A IN THE FILE “BOOK.TXT”. Wap to obtain string from the user and then transfer all vowels to vowels.txt, numbers to digits.txt and all other characters to other.txt.

Abc =open(“student. dat ”,”r”) With open(“student. dat ”,”r”) as abc
Tags