Data File Handling
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.
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
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
a. 3
b. 4
c. 5
d. 6
[CBSE SQP Term–1 MCQs 2021–22]
Ans: b. 4
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:
a. 5
b. 25
c. 26
d. 27
[CBSE SQP Term–1 MCQs 2021–22]
Ans: d. 27
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:
a. 6
b. 7
c. 8
d. 9
[CBSE SQP Term–1 MCQs 2021–22]
Ans: b. 7
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
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:
3. The mkdir() method of the os module to create directories in the current directory.
Syntax: os.mkdir("newdir")