0% found this document useful (0 votes)
2 views13 pages

Datafilepythonn 1

The document discusses data files in Python, explaining their necessity for storing data permanently on hard disks. It outlines the two types of files (text and binary), their differences, and various operations that can be performed on them, such as reading, writing, and appending data. Additionally, it covers file handling functions, modes for opening files, and the use of the Pickle module for handling mixed data types.

Uploaded by

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

Datafilepythonn 1

The document discusses data files in Python, explaining their necessity for storing data permanently on hard disks. It outlines the two types of files (text and binary), their differences, and various operations that can be performed on them, such as reading, writing, and appending data. Additionally, it covers file handling functions, modes for opening files, and the use of the Pickle module for handling mixed data types.

Uploaded by

lalitha08021982
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DATA FILES IN PYTHON

WHY DATA FILES ????????????????

As we know whenever we enter data while running programs, it is not saved


anywhere and we have to enter it again when we run the program again. So to
store required data permanently on hard disk (Secondary Storage Device) we
need to store it in File.

Note : File is a Stream or sequence of bytes /characters

Python Data Files can be of two types

1. Text File (By default it creates file in text Mode


2. Binary File

Difference between Text and Binary Files


[Link]. Python Text File Python Binary Files

1. Consists Data in ASCII (Human readable Consists Data in Binary form


form.
2. Suitable to store Unicode characters also Suitable to store binary data such as
images, video files , audio files etc.
3. Each line in Text file is terminated with a There is no EOL character
Special character EOL(end of line)
4. Operation on text files are slower than Operation on binary files are faster as
binary files as data is to be translated to no translation required
binary

Operations on File:

1. How to Handle Data File/ Text File:


Whenever we worked with Data File in Python we have to follow sequence

1.1 Open/Create File


1.2 Read from/Write to file
1.3 Close File

1 [Link] by Sangeeta M Chauhan , PGT CS, KV3


Gwalior
We can do following tasks/operations with python Data File.

a) Creation/Opening of an existing Data File


b) Reading from file
c) Writing to Data File
d) Appending data ( inserting Data at the end of the file)
e) Inserting data (in between the file)
f) Deleting Data from file
g) Copying a file
h) Modification/Updation in Data File.

Functions Used for File Handling

[Link] Function Syntax Use


. Name
1 open() F_obj=open(“File_name” To Open or create a file in
,mode) desired mode
2 close F_obj.close() To Close the file

3 read() F_obj.read() or To read all or specified no. of


F_obj.read(n) characters from file
4 readline() F_obj.readline() or To read a single line or
F_obj.readline(n) specified no. of characters
5 readlines() F_obj.readlines() from
To a line
read in a file
all lines from a file
and returns it in the form of
6 write() F_obj.write(str) listwrite data (of string type)
To
on to the file
7 writelines() F_obj.writelines(LST) To Write Sequence (list/tuple
etc) of strings in a file

Before discussing reading and writing operation let’s understand the


File Modes. Consider the table of modes given below

2 [Link] by Sangeeta M Chauhan , PGT CS, KV3


Gwalior
Here is a list of the different modes of opening a file −
[Link]. Modes & Description

1 r
Opens a file for reading only. The file pointer is placed at the beginning of the
file. This is the default mode.
2 rb
Opens a file for reading only in binary format. The file pointer is placed at the
beginning of the file. This is the default mode.
3 r+
Opens a file for both reading and writing. The file pointer placed at the
beginning of the file.
4 rb+
Opens a file for both reading and writing in binary format. The file pointer
placed at the beginning of the file.
5 w
Opens a file for writing only. Overwrites the file if the file exists. If the file
does not exist, creates a new file for writing.
6 wb
Opens a file for writing only in binary format. Overwrites the file if the file
exists. If the file does not exist, creates a new file for writing.
7 w+
Opens a file for both writing and reading. Overwrites the existing file if the file
exists. If the file does not exist, creates a new file for reading and writing.
8 wb+
Opens a file for both writing and reading in binary format. Overwrites the
existing file if the file exists. If the file does not exist, creates a new file for
reading and writing.
9 a
Opens a file for appending. The file pointer is at the end of the file if the file
exists. That is, the file is in the append mode. If the file does not exist, it
creates a new file for writing.
10 ab
Opens a file for appending in binary format. The file pointer is at the end of
the file if the file exists. That is, the file is in the append mode. If the file does
not exist, it creates a new file for writing.
11 a+
Opens a file for both appending and reading. The file pointer is at the end of
the file if the file exists. The file opens in the append mode. If the file does not
exist, it creates a new file for reading and writing.
12 ab+
Opens a file for both appending and reading in binary format. The file pointer
is at the end of the file if the file exists. The file opens in the append mode. If
the file does not exist, it creates a new file for reading and writing.

3 [Link] by Sangeeta M Chauhan , PGT CS, KV3


Gwalior
Let’s understand the read Operation.
Look at the following Two Screen shots (1st is Showing Code whereas 2nd is
showing different outputs) where comparison among different type of read
operation have shown. Here you find comparison among read(),readline() and
readlines() functions .
Screenshot 1

Showing
Code with
different
types of
read
operation

Screenshot 2

Showing
Output of
different
types of
read
operations

4 [Link] by Sangeeta M Chauhan , PGT CS, KV3


Gwalior
CODING OUTPUT & EXPLAINATION

f=open("[Link]","r")
contents=[Link]()
print(contents)
[Link]()
Look, it read all the characters from the file
[Link]

f=open("[Link]","r")
contents=[Link](70)
print(contents)
[Link]() Here it has read first 70 characters from the file
[Link]

f=open("[Link]","r")
contents=[Link]()
print(contents)
[Link]() Here it has read Only first linefrom the file [Link]

f=open("[Link]","r")
contents=[Link](60)
print(contents)
[Link]() Here it has read first 60 characters from the first
line of the file [Link]

f=open("[Link]","r")
contents=[Link]()
print(contents)
[Link]()
Here it has read all the lines from the file [Link]

5 [Link] by Sangeeta M Chauhan , PGT CS, KV3


Gwalior
Now let’s discuss write Operation on file
Look at the following Two Screen shots (1st is Showing Code whereas 2nd is
showing different outputs) where comparison among different type of write
operations have shown. Here you find comparison between write(),writeline()

In the above code we have created 4 different files namely Nw_File1.txt,


Nw_File2.txt, Nw_File3.txt, Nw_File4.txt.

In the Nw_File1.txt
we are writing String
Case 1 with write() function.
See the output

6 [Link] by Sangeeta M Chauhan , PGT CS, KV3


Gwalior
In the Nw_File2.txt
we have wrote list of
String without \n
Case 2 using writelines()
function

In the Nw_File3.txt we
have wrote list of String
with\n using write()
function
Case 3

In the Nw_File4.txt we
have wrote tuple of
Strings with \n String
using write() function
Case 4

In the previous cases whenever we run the code again previously written
contents will be overwritten. If we want to add contents after the
previously added contents then we need to open file in append mode.

See Example “[Link]”:

7 [Link] by Sangeeta M Chauhan , PGT CS, KV3


Gwalior
Output of the above code

Now reading the contents of file created by above code

File : “[Link]”

Output will be

Now If we Run the file “[Link]” again it will add new records after the
previously added records.

Have a look ,(This time we are adding 3 records)

8 [Link] by Sangeeta M Chauhan , PGT CS, KV3


Gwalior
After running the “[Link]” again, Output will be

2. Binary Files in Python

Why Binary Files ?????????????

Since binary files store data after converting it into binary language
(0s and 1s), there is no EOL character. This file type returns bytes.
This is the file to be used when dealing with non-text files such as
images or exe.

2.1 Reading and Writing list from/to binary file

Case 1: Writing list on Binary File

list1 = [23, 43, 55, 10, 90]

newFile = open("binaryFile", "wb")


Conversion of list into bytes is
[Link](bytearray(list1)) required

[Link]()

9 [Link] by Sangeeta M Chauhan , PGT CS, KV3


Gwalior
Case 2: Reading list from binary File

newFile = open("binaryFile", "rb")

list1=list([Link]()) Conversion of binary data into list is


required
[Link]()

print(list1)

2.2 Reading and Writing pdf file from/to binary file

file = open("[Link]", "wb") # file to write to

file2 = open("ComputerSc_NewXI.pdf", "rb") # file to read from

bin_cont = [Link]()

[Link](bin_cont)

[Link]()

[Link]()

Above Code will make another copy of ComputerSc_NewXI.pdf with


new name [Link]

2.3 Reading and Writing image file from/to binary file

file1 = open("[Link]", "rb") # file to read from

file2 = open("Nw_TREE.jpg", "wb") # file to write to

bin_cont = [Link]()

[Link](bin_cont)

[Link]()

[Link]()

10 [Link] by Sangeeta M Chauhan , PGT CS, KV3


Gwalior
Above Code will make another copy of ComputerSc_NewXI.pdf with
new name [Link]

Above Code will make another copy of image file [Link] with new name
Nw_TREE.jpg

2.3 Reading and Writing Sequences(Dictionary, lists etc) with


mixed data type from/to binary file

To write mixed type of Data we need to import Pickle module. Pickling


means converting structure into byte stream before writing the data
into file
Its two main methods are:

1. pickle .dump() : To write the Object into the file


Syntax : [Link](object_to_write,file_object)
2. pickle .load() : To read the Object from the file
Syntax : container_obj=[Link](file_object)

Lets Understand it more clearly through examples


Case 1: To Write /Read List

Output

11 [Link] by Sangeeta M Chauhan , PGT CS, KV3


Gwalior
Case 2: To Write /Read Dictionary

Output

Case 3: To Write /Read Two different Dictionary

12 [Link] by Sangeeta M Chauhan , PGT CS, KV3


Output
Gwalior
3. ABSOLUTE AND RELATIVE PATHS
 We all know that the when we create python files, they are kept in
default directory which are also known as folders.
 At the time of Program run Python searches current(default)
directory.

Path : is the general form of the name of a file or directory, specifies a


unique location in a file system

Relative Path : A relative path defines a location that is relative to the


current directory or folder. For example, the relative path to
a file named "[Link]" located in the current directory is simply the
filename, or "[Link]"

Absolute Path : An absolute path refers to the complete details


needed to locate a file or folder, starting from the root element and
ending with the other subdirectories

If we want to know the Current Working Directory We have OS module


(Operating System )which provides many such functions which can be used
to work with files and directories and a function getcwd( ) function can be
used to identify the current working directory like this :

>>> import os
>>>curr_dir=[Link]()
>>> print(curr_dir)
Output C:\Users\Sangeeta Chauhan\AppData\Local\Programs\Python\Python36-32

4. Standard FileStreams
standard streams are preconnected input and output communication
channels between a computer program and its environment when it begins
execution.
The three input/output (I/O) connections/Streams are :

i. Standard input (stdin),  [Link] (read from standard input)


ii. Standard output(stdout) [Link] (Write to Standard Output Device)
iii. Standard error (stderr). [Link] (Contains Error Messages)

13 [Link] by Sangeeta M Chauhan , PGT CS, KV3


Gwalior

You might also like