0% found this document useful (0 votes)
3 views

Data File Handling

Uploaded by

anikeit
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Data File Handling

Uploaded by

anikeit
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Ch – 4: Data File Handling

Data Files
Data Files – Files that store data pertaining to a specific application, for later use.
Types of Text Files
(i) Regular Text Files:
Why use data files
Newline character ends a line and Text translation: Yes
1. To store data in organized manner
File extension: .txt
2. To store data permanently
3. To access data faster
(ii) Delimited Text Files
4. To search data faster
A character is used to separate the values (Tab/Comma)
5. To easily modify data later on.
a. TSV (Tab Separated Values files) - .tsv or .csv
b. CSV (Comma Separated Values files) - .csv
Types of Data Files
Python allows us to create and manage two types of files:
A. Text Files
1. A file whose contents can be viewed using a text editor.
2. Simply a sequence of ASCII or Unicode characters.
3. Python programs content written in text editors are examples of text files
Ex - Python programs, .txt, .rtf, .csf etc.
B. Binary Files
1. A file that stores data in the same way as stored in the memory.
2. The .exe, .mp3 file, image files are some of the examples of binary files
3. We can't read a binary file using a text editor.

Text Files Binary Files


1. Stores information in the form of a stream 1. Stores the information in the form of a
of ASCII or Unicode characters. stream of bytes.
2. Each line is terminated (delimiter) with a 2. There is no delimiter for a line
special character called EOL character.
3. Some internal translations take place 3. No translations occur in binary files.
when this EOL (End of Line) character.
4. Less prone to get corrupt as change 4. Can easily get corrupted on even single bit
reflects as made and cannot be undone change.
5. Share only plain text in a file. 5. Can store different types of data (audio,
text, image) in a single file
6. Widely used file format and can be 6. Developed for an application and can be
opened in text editor. opened in application only. (Not in T.E.)
7. Mostly, .txt, .rtf are used as extensions to 7. Can have application defined extension.
text files.
8. Its Bits represent character. 8. Its Bits represent a custom data.
4.5 Reading from a file
1. Read(): To read the entire data from the file; starts from the cursor up to the end of the
file.

2. Read(n): To read 'n' characters from the file, starting from the cursor
if the file holds fewer than 'n' characters, it will read until the end of the end of the file.
If no 'n' is specified, reads the entire file.
Reads at 'n' bytes

3. Readline(): To read only one line from the file; starts reading from the cursor; if the file
holds fewer than 'n' characters, it will read until the end of the file.

Returns the read bytes in the form of a string ending with ln (line) character or returns a
black string if no more bytes are left for reading in the file

4. readlines(): To

Reads all lines and returns them in a list


Removing Whitespaces after Reading from File

1. Removing EOL '\n' character from the line read from the file

2. Removing the leading whitespaces from the line read from the file
Output Based

f = open("LINES.txt","r")
fr = f.read()
print("f.read()\n", fr,"\n")
print("len(f.read())\n", len(fr),"\n")
print("type(f.read())\n", type(fr),"\n")

f = open("LINES.txt","r")
frls = f.readlines()
print("-"*70)
print("f.readlines()\n", frls,"\n")
print("len(f.readlines())\n", len(frls),"\n")
print("type(f.readlines())\n", type(frls),"\n")

f = open("LINES.txt","r")

frl = f.readline()
print("-"*70)
print("f.readline()\n", frl)
print("len(f.readline())\n", len(frl),"\n")
print("type(f.readline())\n", type(frl))

Output:
Questions

Q. (Q27) Suppose content 'Myfile.txt' is:

Twinkle Twinkle litter star


How I wonder what you are
Up above the world so high
Like a diamond in the sky

What will be the output of the following code?


myfile = open("Myfile.txt")
data = myfile.readlines()
print(len(data))
myfile.close() .

a. 3
b. 4
c. 5
d. 6
[CBSE SQP Term–1 MCQs 2021–22]
Ans: b. 4

Q. (Q40) Suppose content 'Myfile.txt' is:

Humpty Dumpty sat on a wall


Humpty Dumpty had a great fall
All the king's horses and all the king's men
Couldn't put Humpty together again

What will be the output of the following code?


myfile = open("Myfile.txt")
record = myfile.read().split()
print(len(record))
myfile.close() .

a. 24
b. 25
c. 26
d. 27
[CBSE SQP Term–1 MCQs 2021–22]
Ans: c. 26
Q. (Q42) Suppose content 'Myfile.txt' is:

Honesty is the best policy.

What will be the output of the following code?


myfile = open("Myfile.txt")
x = myfile.read()
print(len(x))
myfile.close() .

a. 5
b. 25
c. 26
d. 27
[CBSE SQP Term–1 MCQs 2021–22]
Ans: d. 27

Q. (Q43) Suppose content 'Myfile.txt' is:

Culture is the widening of the mind and of the spirit.

What will be the output of the following code?


myfile = open("Myfile.txt")
x = myfile.read()
y = x.count('the')
print(y)
myfile.close() .

a. 2
b. 3
c. 4
d. 5
[CBSE SQP Term–1 MCQs 2021–22]
Ans: b. 3
Q. (Q45) Suppose content 'Myfile.txt' is:

Ek Bharat Shreshtha Bharat.

What will be the output of the following code?


myfile = open("Myfile.txt")
vlist = list("aeiouAEIOU")
vc = 0
x = myfile.read()
for y in x:
if (y in vlist):
vc += 1
print(vc)
myfile.close() .

a. 6
b. 7
c. 8
d. 9
[CBSE SQP Term–1 MCQs 2021–22]
Ans: b. 7

Q. (Q46) Suppose content 'Myfile.txt' is:

Twinkle Twinkle litter star


How I wonder what you are
Up above the world so high
Like a diamond in the sky
Twinkle Twinkle litter star

What will be the output of the following code?


myfile = open("Myfile.txt")
line_count = 0
data = myfile.readlines()
for line in data:
if line[0] == 'T':
line_count += 1
print(line_count)
myfile.close() .

a. 2
b. 3
c. 4
d. 5
[CBSE SQP Term–1 MCQs 2021–22]
Ans: a. 2
Q. (Q48) Assume the content of text file, 'student.txt' is:

Arjun Kumar
Ismail Khan
Joseph B
Hanika Hiran

What will be the data type of data_rec?


myfile = open("Myfile.txt")
data_rec = myfile.readlines()
myfile.close() .

a. string
b. list
c. tuple
d. dictionary
[CBSE SQP Term–1 MCQs 2021–22]
Ans: b. list

4.9

Methods of os module
The OS module in python provides functions for interacting with the operating system

Methods available in the OS module (standard module) which can be used file operations:

1. The rename() method used to rename the file.


Syntax: os.rename (current_file_name, new_file_name)

2. The remove() method used to delete the file.


Syntax: os.remove(file_name)

3. The mkdir() method of the os module to create directories in the current directory.
Syntax: os.mkdir("newdir")

4. The chdir() method to change the current directory.


Syntax: os.chdir("newdir")

5. The getcwd() displays the current working directory.


Syntax: os.getcwd()

6. The rmdir() deletes the directory.


Syntax: os.rmdir("dirname")
**

You might also like