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

CS12 FileHandlingPythonPart2

This document provides an overview of file handling in Python, focusing on binary files and the Pickle module for serialization and deserialization of objects. It outlines the steps to use the Pickle module, including methods like pickle.dump() and pickle.load(), as well as the differences between standard input/output streams and file paths. Additionally, it discusses absolute and relative paths, the os module for file operations, and attributes of file objects.

Uploaded by

vr418273
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 views25 pages

CS12 FileHandlingPythonPart2

This document provides an overview of file handling in Python, focusing on binary files and the Pickle module for serialization and deserialization of objects. It outlines the steps to use the Pickle module, including methods like pickle.dump() and pickle.load(), as well as the differences between standard input/output streams and file paths. Additionally, it discusses absolute and relative paths, the os module for file operations, and attributes of file objects.

Uploaded by

vr418273
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
You are on page 1/ 25

File Handling in Python

PART – II
(Based on Binary Files)

1
Binary File ? … stores information in the same format as it is held in the memory

Pickle module can be used to store and retrieve any object (integer, float,
strings, lists, dictionary etc. ) in a binary file.
Pickle module is thus used for serializing and de-serializing Python objects.

Serialization (Pickling) refers to the process of converting an object in memory to a byte stream that can be
stored on disk.
Deserialization (Unpickling) converts the byte stream (generated through pickling) back into python object.

2
Steps to use Pickle module
1. Import the module file_object = open(filename , access_mode )

2. Open the file

3. Use methods pickle.dump() / pickle.load() to write / read from and from binary files.

4. Close the file


fileobject. close( )

Revisiting… file access_modes


access_mode Position of file pointer
r, r+, rb , rb+ , r+b Beginning of the file
w , w+ , wb , wb+ , w+b Beginning of the file ( after truncating file )
a, a+, ab , ab+ , a + b End of file in existing file, else a new file is created
3
Pickle module Methods
Method Syntax Description & Example
pickle.dump() dump(object, fileobject) Writes the object in file, which is opened in binary access mode.
# Example: writing a List to a Binary File
import pickle
l = [1,2,3,4,5,6]
file = open('list.dat', 'wb') # b in access mode is for binary file
pickle.dump(l,file) # writing content to binary file
file.close()
pickle.load() object = load(fileobject) Reads the object from pickle file.
Note : we need to call load for each time dump was called.
# Example: Reading a List from a Binary File
import pickle
l=[]
file = open('list.dat', 'rb') # b in access mode is for binary file
l = pickle.load(file) # reading content from binary file
print(l)
file.close()
4
Pickle module – dump( ) and load ( ) Example
Write to a binary file

append to a binary file

read from a binary file

Note: To distinguish a data file from


pickle file, we may use a different
extension of file .pk / .pck / .pickle
are commonly used.
5
CREATE A BINARY FILE
[ {},{} ]

[ {'rollno': 11,
'name': 'aa',
'stream':
'science',
'percent': 97.0} ]

6
APPEND TO A BINARY FILE
[ {},{}…]

[ {'rollno': 22,
'name': ‘bb',
'stream':
‘commerce’
'percent': 95} ]

7
DISPLAY FROM A BINARY FILE
L is a list of
dictionary items
[{},{},{}]

OR

Output
Output
x
x
x

8
SEARCH & DISPLAY FROM A BINARY FILE
science

Output

9
UPDATE A BINARY FILE

33

95

33
95 L

95

10
DELETE FROM A BINARY FILE

22

22
L
0
1
2

11

Note: List.index(item) example


Calling Functions from MENU

12
STANDARD STREAMS
Standard streams* are file objects, which get automatically connected to program's standard device(s), when
we start python.

The standard streams available are:


• stdin ( standard input stream )
• stdout ( standard output stream )
• stderr ( standard error stream )

Programs make output to "stdout" and "stderr" . By default, both are written to the screen.
Programs take input from “stdin”. By default, input is taken from the keyboard.

In order to work with standard I/O stream, we need to import sys module.
i.e. Python uses sys.stdin , sys.stdout and sys.stderr streams

13

streams* are referred to information streams


Using sys.stdin to read
Method Meaning Example
sys.stdin.read([size]) If size is not specified, it reads
multiple lines from standard input
buffer, till the user presses ctrl + D.
If n is specified, it reads n bytes from
the input buffer. Input terminates with
press of Enter key.
It returns a string.
sys.stdin.readline() It reads a single line of input from
standard input buffer.
It returns a string.
sys.stdin.readlines([size]) same as read([size]) except that
It returns a List of string(s)

14
Example of using sys.stdin to read

INPUT BUFFER
hello world\n
display’s only first 5 characters

INPUT BUFFER
world\nis everyone ok?\n

Notice the output contains


remaining bytes of previous
input and new input

15
Difference between input( ) and readline( )
input( ) readline( )
The input takes input from the user but The readline() also takes input from the
does not read escape character (Enter key user but also reads the escape character (
or \n character ) nter key or \n character )
It has a prompt that represents the default Readline has a parameter named size,
value before the user input. Which is a non-negative number, it actually
defines the bytes to be read.
Example: Example:
S = input(‘Enter a value: ‘) import sys
S = sys.stdin.readline( )

16
Using sys.stdout and sys.stderr to write
Method Meaning Example
sys.stdout.write( s ) It writes a single string to the standard
sys.stderr.write( s ) output buffer.

Output:

sys.stdin.writelines( s ) It writes an iterable (string / List of


sys.stderr.writelines( s ) strings ) to the output buffer

Output:

17
Example of using sys.stdin, sys.stdout and sys.stderr
strive for success

not for perfection

18

Difference between print( ) and write( ) : print() displays output on screen and puts a \n after the output.
Write() does not put \n after displaying output so subsequent outputs from print would appear in the same line.
File Path path + name of the file
Syntax of open() :
file_object = open(filename [, access_mode] )
If no path is included then, the file is assumed to be in the current working directory (CWD)
file = open(‘story.txt’) C:

Users
To open a file from other than CWD, path along name of file has to specified
Python allows using both forward slash "/" and backslash “\” in the file path. abc

Desktop
1st method: file = open( 'C:/Users/abc/Desktop/story.txt‘)
Story.txt
2nd method: file = open('C:\\Users\\abc\\Desktop\\story.txt‘)
If using backslash, because it is a special character in Python, you must remember to escape it’s every instance

3rd method: file = open(r'C:\Users\abc\Desktop\story.txt‘)


Alternatively, you can prefix the entire file name string with the rawstring marker "r":
That way, everything in the string is interpreted as a literal character, and you don't have to escape every
backslash.
19

A directory refers to a folder on a computer. Directories are hierarchical which means that they can exist within
other folders as well as have folders exist within them.
What is the Home Directory?
The home directory on a computer is a directory defined by your operating system. The home directory is the
primary directory for your user account on your computer. Your files are by default stored in your home directory.
On Windows, the home directory is typically C:\Users\your-username
On Mac and Linux, the home directory is typically /home/your-username

What is a Working Directory?


The working directory refers to the directory (or location) on your computer that a the tool (Python) assumes is
the starting place for all paths that you construct or try to access.
When you execute your script, your CWD is set to the directory where your script is. Therefore, you can refer to
a file in a script by its name only provided that the file and the script are in the same directory. :
To check your CWD, type the following in Python shell

CWD on my system
20
Absolute Paths
An absolute path is a path that contains the entire path to the file or directory that you need to access. This path
will begin at the home directory of your computer and will end with the file or directory that you wish to access.
Absolute paths ensure that Python can find the exact file on your computer.
file = open( 'C:/Users/abc/Desktop/story.txt‘)
An absolute path, always begins with the root folder (C: or D: etc on Windows)

Relative Paths
A relative file path is relative to the program’s current working directory. It interpreted from the perspective your
current working directory.
There are also the dot (.) and dot-dot (..) folders.
These are not real folders but special names that can be used in a path.
A single period (“dot”) for a folder name is shorthand for “this directory.”
Two periods (“dot-dot”) means “the parent folder.”

If Users is CWD, then the file story.txt can be referred ad:


File =open(‘abc/Desktop/story.txt’)

21
ABSOLUTE VS. RELATIVE PATHS

(Source of picture: https://automatetheboringstuff.com/chapter8/)


22
os module
Files are always stored in current folder / directory by default. The os (Operating System) module of
python provides various methods to work with file and folder / directories.
Some useful methods of OS module:
1. getcwd() to know the name of current working directory
2. path.abspath(filename) will give us the complete path name of the data file.
3. path.isfile(filename) will check, whether the file exists or not.
4. remove(filename) will delete the file. Here filename has to be the complete path of file.
5. rename(filename1,filename2) will change the name of filename1 with filename2

23
fileobject attributes
Some useful attributes of fileobject
1. file.closed returns True if file is closed
2. file.mode returns the mode, with which file was opened.
3. file.name returns name of the file associated with file object while opening the file.
Example:

Note: We have already covered various methods of fileobject like open() , close() , read() , readline() ,
readlines() , write() , writelines()
24
Next > File Handling Part -3 (Based on CSV Files)

25

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