0% found this document useful (0 votes)
29 views6 pages

Lojj

Uploaded by

paldonny55
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views6 pages

Lojj

Uploaded by

paldonny55
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

DIFFERENCE BETWEEN TEXT FILE AND BINARY FILE

Text File Binary File


Its Bits represent character. It represents a custom data.

Less prone to get corrupt as change Can easily get corrupted, corrupt on re-
flects as soon as made and can be even single bit change
undone.

Store only plain text in a file. Can store different types of data

Widely used file format and can be Developed for an application and
can be opened in any text editor. can be opened in that only.

opened in any text editor. Can have any application defined

Mostly .txt and .rtf are used as

extensions. extensions to text files.

seek() method
In Python, seek() function is used to change the position of the File Handle to a given specific posi-
tion. File handle is like a cursor, which defines from where the data has to be read or written in the
file.

Syntax: f.seek(offset, from_what), where f is file pointer


Parameters:
Offset: Number of positions to move forward
from_what: It defines point of reference.
Returns: Return the new absolute position.

The reference point is selected by the from_what argument. It accepts three values:

• 0: sets the reference point at the beginning of the file


• 1: sets the reference point at the current file position
• 2: sets the reference point at the end of the file
By default from_what argument is set to 0.

Which statement will read 5 characters from Q2 Which function open file in python?
a file? A) open()
A) f.read() B) new()
B) f.read(5) C) Open()
C) f.reads(5) D) None of the above
D) None of the above
Q3 Processing of Text file is faster then binary Q4 which mode create new file if the file does
files(T/F) not exist?
A) write mode
B) Append mode
C) Both of the above
D) None of the above
Q5 readlines() method return Q6 Which of the following will read entire con-
A) String tent of the file?
B) List A) f.reads()
C) Dictionary B) f.read()
D) Tuple C) f.read(all)
D) f.read(*)
Q7 Which of the following options can be Q8 Which function is used to write data in bi-
used to read the first line of a text file da- nary mode?
ta.txt? A) write
A) f=open(‘data.txt’); f.read() B) write-ins
B) f=open(‘data.txt’,’r’); f.read(n) C) pickle
C) myfile=open(‘data.txt’); f.readline() D) dump
D) f=open(‘data.txt’); f.readlines()
Q9 ____ module is used for serializing and de- Q10 Identify the invalid mode from the follow-
serializing any Python object structure ing
A) pickle A) a
B) unpickle B) r+
C) pandas C) ar+
D) math D) W
Q11 Q12 seek() method is used for random access
import pickle to the file. (T/F)
f=open(‘data.dat’,’rb’)
d= ______ . load(f)
f.close()
A) unpickle
B) pickling
C) pickle
D) pick
Q13 Q14 ____ function returns the current position
The syntax of seek() is :file_object.seek(offset of the file pointer.
[,reference_point] What is the default value of A) get()
reference_point B) tell()
A) 0 C) cur()
B) 1 D) seek()
C) 2
D) 3
Q15 f.seek(10,0) will move 10 bytes forward Q16 Which statement will move file pointer
from beginning of file(T/F) 10 bytes backward from current position.
A) f.seek(-10,0)
B) f.seek(10,0)
C) f.seek(-10,1)
D) None of the above
Q16 which module we use for Q17 Identify the correct statement to open a
remove() file:
rename() A) f=open(“d:\\folder\\naman.txt”)
B) f=open(“d:\\folder\naman.txt”)
C) f=open(“d:folder#naman.txt”)
D) f=Open(“d:\folder\naman.txt”)
Q18 Which of the following mode will open Q19 Select all true statements when a file is
the file in binary and read-only mode. opened using the with statement
A) ‘r’ • The with statement simplifies exception
B) ‘rb’ handling
C) ‘r+’ • The file is automatically closed after leav-
D) ‘rb+’ ing the block, and all the resources that
are tied up with the file are released.
• File reading and writing are faster using
the with statement.
q1 Differentiate between file mode r+ and rb+
Q2 What are files?
A file is a named location on a secondary storage media where data is permanently stored for later
access.
Q3 what are the types of files in Python?
Q4 Write a method COUNTLINES() in python to read lines from text file ‘TESTFILE.TXT’ and dis-
play the lines which are not started with any vowel.
Q5 Write a function ETCount() in Python, which should read each character of text file ‘TEST-
FILE.TXT’ and then count and display the count occurrence of alphabets E and T individually (in-
clude small case and upper case).
Q6 Write a method DISPLAYWORDS() in python to read lines from text file ‘STORY.TXT’ and dis-
play the words , which are less than 4 characters.
Q7 Write a method COUNTHISHER() in python to read content from text file ‘STORY.TXT’ and
count the words His and Her .
Q8 Write a program in python to replace all word “the” by another word “them” in a file “poem.txt”
Q9 write a program in python to display only unique words from the file “story.txt”.
Q10 Write a program in python to count the frequency of each vowels in a file “task.txt”.
Q11 Write a program in Python to read the entire content from file “data.txt” copy the contents to
“story.txt” in uppercase.
Q12 Write a program in Python to read the entire content from file “data.txt” and copy only those
words to “story.txt” which start from vowels.
Q13 Write a program in Python that read the content from file “sumit.txt” and display sum of digits .

ADVANTAGES OF CSV FILES


• The files have a simple structure and are human-readable;
• The data can be easily imported or exported into other programs;
• CSV file format can be easily and accurately compressed;
• The running costs are not high;
• Perfect fit for getting data out of one application and into another one;
Disadvantages of CSV
• There is no distinction between text and numeric values
• Poor support of special characters and control characters
• CSV allows to move most basic data only. Complex configurations cannot be
imported and exported this way

ADVANTAGES OF PICKLE
• Comes handy to save complicated data.
• Easy to use, lighter and doesn’t require several lines of code.
• The pickled file generated is not easily readable and thus provide some security.

AND DISADVANTAGES OF PICKLE

• Languages other than python may not able to reconstruct pickled python objects.
• Risk of unpickling data from malicious sources.

QUES1
Ranjan Kumar of class 12 is writing a program to create a CSV file “user.csv” which will contain
user name and password for some entries. He has written the following code. As a programmer,
help him to successfully execute the given task.
import _____________ # Line 1
def addCsvFile(UserName,PassWord): # to write / add data into the CSV file
f=open(' user.csv','________') # Line 2
newFileWriter = csv.writer(f)
newFileWriter.writerow([UserName,PassWord])
f.close() #csv file reading code
def readCsvFile(): # to read data from CSV file
with open(' user.csv','r') as newFile:
newFileReader = csv._________(newFile) # Line 3
for row in newFileReader:
print (row[0],row[1])
newFile.______________ # Line 4
addCsvFile(“Arjun”,”123@456”)
addCsvFile(“Arunima”,”aru@nima”)
addCsvFile(“Frieda”,”myname@FRD”)
readCsvFile() #Line 5
(a) Name the module he should import in Line 1.
(b) In which mode, Ranjan should open the file to add data into the file
(c) Fill in the blank in Line 3 to read the data from a csv file
(d) Fill in the blank in Line 4 to close the file.
(e) Write the output he will obtain while executing Line 5.

QUES2
A binary file “Book.dat” has structure [BookNo, Book_Name, Author, Price].
i) .Write a user defined function CreateFile() to input data for a record and add to Book.dat .
ii) Write a function CountRec(Author) in Python which accepts the Author name as parameter
and count and return number of books by the given Author are stored in the binary file “Book.-
dat”

QUES3
A binary file “STUDENT.DAT” has structure (admission_number, Name, Percentage). Write a
function countrec() in Python that would read contents of the file “STUDENT.DAT” and display the
details of those students whose percentage is above 75. Also display number of students scoring
above 75%

QUES4
Ronit has a CSV file “marks.csv” which has name, class and marks separated by comma. He is
writing a Python program to copy only the name and class to another CSV file “class.csv”. He has
written the following code. As a programmer, help him to successfully execute the given task.

QUES5
import csv
file = open('class.csv', a , newline="");
writer = csv.writer(file)

b open('marks.csv') as csvfile:
data = csv. c (csvfile)
for row in data:
writer.writerow([ d , e ])

file.close()
1. In which mode should Ronit open the file to make a new file?
2. Which Python keyword should be used to manage the file stream?
3. Fill in the blank in Line 3 to read the data from a csv file.
4. Fill in the blank to write name into marks.csv
5. Fill in the blank to write class into marks.csv.

QUES6
What is the advantage of using a csv file for permanent storage?
Write a Program in Python that defines and calls the following user defined functions:
(i) ADD() – To accept and add data of an employee to a CSV file ‘record.csv’. Each record con-
sists of a list with field elements as empid, name and mobile to store employee id, employee
name and employee salary respectively.
(ii) (ii) COUNTR() – To count the number of records present in the CSV file named ‘record.csv’.

QUES7
Give any one point of difference between a binary file and a csv file. Write a Program in Python
that defines and calls the following user defined functions: (i) add() – To accept and add data of an
employee to a CSV file ‘furdata.csv’. Each record consists of a list with field elements as fid, fname
and fprice to store furniture id, furniture name and furniture price respectively. (ii) search()- To dis-
play the records of the furniture whose price is more than 10000.

QUES8
Write a code that reads from a file “sales.dat” which has following information [itemcode, amount]
Read from the file and find the sum of the amount.

QUES9
A binary file “salary.DAT” has structure [employee id, employee name, salary]. Write a function
countrec() in Python that would read contents of the file “salary.DAT” and display the details of
those employee whose salary is above 20000.

QUES10
A file sports.dat contains information in following format [event, participant].
Write a program that would read the contents from file and copy only those records from sports.dat
where the event name is “Athletics” in new file named Athletics.dat

QUES11
A binary file “STUDENT.DAT” has structure [admission_number, Name, Percentage]. Write a func-
tion countrec() in Python that would read contents of the file “STUDENT.DAT” and display the de-
tails of those students whose percentage is above 75. Also display number of students scoring
above 75%.

QUES12
Aman is a Python programmer. He has written a code and created a binary file record.dat with em-
ployeeid, ename and salary. The file contains 10 records. He now has to update a record based on
the employee id entered by the user and update the salary. The updated record is then to be writ-
ten in the file temp.dat. The records which are not to be updated also have to be written to the file
temp.dat. If the employee id is not found, an appropriate message should to be displayed. As a
Python expert, help him to complete the following code based on the requirement given above:
import _______ #Statement 1
def update_data():
rec={}
fin=open("record.dat","rb")
fout=open("_____________") #Statement 2
found=False
eid=int(input("Enter employee id to update their salary :: "))
while True:
try:
rec=______________ #Statement 3
if rec["Employee id"]==eid:
found=True
rec["Salary"]=int(input("Enter new salary :: "))
pickle.____________ #Statement 4
else:
pickle.dump(rec,fout)
except: break
if found==True:
print("The salary of employee id ",eid," has been updated.")
else:
print("No employee with such id is not found")
fin.close()
fout.close()
(i) Which module should be imported in the program? (Statement 1)
(ii) Write the correct statement required to open a temporary file named temp.dat. (State-
ment 2)
(iii) Which statement should Aman fill in Statement 3 to read the data from the binary file,
record.dat and in Statement 4 to write the updated data in the file, temp.dat?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy