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

Lec 08m - File Handling

Uploaded by

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

Lec 08m - File Handling

Uploaded by

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

Chapter 8

File Handling

File Handling
 File Introduction  Reading a File
 Text File and Binary File  Reading a File - File Content
 Opening a File  Reading a File Line by Line
 File Opening Modes  Cursor Positioning Methods
 Closing a File  Reading and Writing File
 Statement of “with open() as  Renaming a File
 Writing a File  Deleting a File
 Appending a File  Binary File Operations
 Text File Operations
 Serialization in Python
2

1
File Introduction
 Primary storage (e.g. RAM) is located inside the computer, to
retain digital data that is in active use.
 However, the primary storage is volatile, it loses data as soon
as the computer is turned off.
 File is a named location on the secondary storage (e.g. hard
disk) that used to store the data permanently.
 There are two types of file: text file and binary file.

Text File and Binary File


Text File Binary File
The content can be viewed by text editor. The content can not be viewed by text editor.
It consists of a series of bytes that It consists of a series of raw bytes that
represent the ASCII values of characters. represent the actual content (image, audio,
video, compiled file, executable files)
Less prone to get corrupt if change is Can easily get corrupted even single bit is
made and the change can be undone. changed
Each line of text is terminated with a There is no terminator for a line of raw bytes.
special character - EOL (End of Line)
which is the new line character (‘\n’).
File extension: txt, rtf File extension: jpeg, mp3, avi, obj, exe
4

2
Opening a File
 A file must be opened before the operation of read, write,
edit can be done.
 When a file is opened, a file relevant structure is created
in memory space to prepare the content storing.
 File operation generally takes place in the following order:
 Open a file
 Read or write operation
 Close the file

Opening a File (cont...)


 The open() method:
file_object = open(file_name, access_mode)

 It opens the given filename (from the current directory ) in the


specified access mode
 The access mode refers to how the file is used once its opened
 The default mode is reading mode.
"r" Read mode Read the content of the file
"w" Write mode Remove existing content and write something into the file
"a" Append mode Remain existing content and write something to the end of the file

3
Example
 f = open("readme.txt") # open file in current directory
 f = open("C:/MMU/readme.txt") # specifying full path

 f = open("test.txt") # equivalent to 'r'


 f = open("test.txt",'w’) # write in text mode
 f = open("img.bmp",'r+b’) # read and write in binary mode

File Opening Modes


Action File Mode Type of File File Offset Position
Read <r> text file Beginning of the file
<rb> binary file Beginning of the file
Read + Write <r+> or <+r> text file Beginning of the file
<rb+> or <+rb> binary file Beginning of the file
Write <w> text file The file content is overwritten Beginning of the file
if the file exists. If not,
<wb> binary file Beginning of the file
creates a new file for writing.
Write + Read <w+> or <+w> text file The file content is overwritten Beginning of the file
if the file exists. If not,
<wb+> or <+wb> binary file Beginning of the file
creates a new file for reading
and writing.

4
File Opening Modes (cont..)
Action File Mode Type of File File Offset Position
Append <a> text file If the file does not exist, then End of the file
a new file will be created.
<ab> binary file End of the file

Append + <a+> or <+a> text file If the file does not exist, then End of the file
Read a new file will be created.
<ab+> or <+ab> binary file End of the file

File Opening Modes (cont..)

10
Source: https://www.semicolonworld.com/question/42943/python-open-built-in-function-difference-between-modes-a-a-w-w-and-r

5
Closing a File
 A file need to be closed after read/write operations to
release the memory allocated to the buffer.
 When a file is closed, the buffer is flushed and the
content written in the file will persist to the file.
 If the file is not closed, it will cause the resource leak
and eventually crash due to insufficient memory.
 In Python, file is automatically flushed while the file is
closed.

11

Closing a File (cont..)


 The close() method:
 It closes the file and frees the memory space acquired
by the file.
file1 = open("test.txt","r")
file1.close()
 This method is not entirely safe.
 If programmer forget to use close() method, the file will
remind open without closing.
 A safer way is to use "with open() as" statement.

12

6
Statement of “with open() as”
 The best way to close a file is by using the "with open() as"
statement.
with open (file_name, access_mode) as file_object:

 File that is opened using with statement will be closed


automatically when the block inside the with clause is exited.
 Programmer no need to close the file explicitly using close()
method. Python will close the file internally.

13

Writing a File
 To write something into a file, the file need to be
opened in either write (w) or append (a) modes.
 When the file is opened in the write mode:
 It creates a new file in the current directory if the file
does not exist.
 It overwrite the file content if the file already exists.
 When the file is opened in the append mode:
 It creates a new file in the current directory if the file
does not exist.
 The new data is written as the last line if the file already
exists. 14

7
Writing a File (cont..)
 write() method
 The newline character (\n) is needed at the end of the
sentence to mark the end of line.

 It returns the number of characters written to the file.


(when the file is not closed).

15

Writing a File (cont..)


 write() method
 If numeric data is written to the file, it need to be
converted into string before written to the file.

file = open('test1.txt','w')
num = 166
file.write("Number is ")
file.write(str(num))
file.close()

 The write() writes the data onto a buffer.


 When close() is executed, the content from the buffer is
moved and saved to the file. 16

8
Writing a File (cont..)
 writelines() method
 It writes a sequence of strings to a file.
 It can be iterable sequence types such as list, tuple
containing the strings.

fo = open("program.txt", "w")
print ("The file name:", fo.name)
seq = ["Python\n", "Java\n", "C++\n"]
fo.writelines(seq)
fo.close()

17

Appending a File
 To append the file content
 File is opened through the open() method, the access
mode is set to "a".
 write() and writelines() can be used.

file = open('test.txt','a')
file.write("\nThis sentence will be added in the last line")
file.close()

18

9
Reading a File
 read() method
 It reads the content of the file.
 read(n) – it reads the n number of bytes from the file.

19

Reading a File (cont..)


 readline() method
 It reads a single line from a file where the line
terminates with a newline (\n) character.
 readline(n) – it reads n bytes but never exceeds one line.

20

10
Reading a File (cont..)
 readlines() method
 It reads all the lines and returns a list containing the string
forms of the read lines.
 readlines(n) – it read n bytes from the file, and n is
rounded up to include the entire line.

21

Reading a File - File Content


 If the file is in small size, readlines() can be used.
 readlines() reads the whole file content to memory,
then splits it into separate lines and returns a list of all
lines in the file.
 All the lines in list except the last one, will contain new
line character at the end.

22

11
Reading a File - File Content (cont..)

• The readlines() function returns a list of lines in file.


• The list is iterated and strip() is used to strip the new line character then print the line.
• However, it consume a lot of memory if the file size is large.
• It is advisable to avoid this method in case of large files.

23

Reading a File Line by Line


 While reading a large file, efficient way is to read file line
by line instead of fetching all data in one go.

24

12
Reading a File Line by Line (cont..)
 Using the "with open() as" statement to close the file
automatically

In this case when control comes out of with block then file will be automatically closed.
Even if it came out of block due to some exception.

25

Cursor Positioning Methods


 When a file is opened, the cursor points to the beginning of
the file. Read / Write operation will happen from the start.
 seek() method – it changes the cursor to a specific position
of the file object.

26

13
Cursor Positioning Methods (cont..)
 tell() method – it returns the current position of the cursor
of the file object

27

Reading and Writing File


r+ and w+ operation

28

14
Reading and Writing File (cont..)
r+ and w+ operation

29

Reading and Writing File (cont..)


r+ and w+ operation

30

15
Reading and Writing File (cont..)
r+ and w+ operation

31

Renaming a File
 Renaming a file in Python can be done using the os
module and it needed to be imported to perform such
tasks.
 The .rename() method takes two arguments:
 The current file name, passed as a string type.
 The renamed file name, to be passed as a string type.

• 'test.txt' is the file’s current name.


• 'testA.txt' is the new file name.
• Since rename the file does not change the
content of the file, the file is no need to be
opened to perform the renaming task.
32

16
Deleting a File
 The file is removed from a directory using os module
from Python.
 remove() method is used to perform the task.
 This method takes a single argument as string type
which is the file name.

33

Deleting a File (cont..)

Since the file is deleted, the file can not be opened again and as a
result, error appear.

34

17
Binary File Operations
Binary File (Read and Write)

The list has been converted from The contents of the bny
something human can read into variable stores the bytes and
something that is more efficient it is converted back into a list
for a computer system to handle. using the list() function.
35

Binary File Operations (cont..)


Binary File (Reading a picture file)

36

18
Binary File Operations (cont..)
Binary File (Read and Write - ASCII value)

37

Text File Operations


Copying a Text File
file1 = open("test.txt", "r")
file2 = open("test3.txt", "w") content

fo = file1.readline()
while fo: test.txt test3.txt
file2.write(fo)
fo = file1.readline()

file1.close()
file2.close()

38

19
Serialization in Python
 Object Serialization – to store the data structures (list,
tuple, dictionary and set) in memory, they need to be
converted into a sequence of bytes that the computer
can understand.

 Object Deserialization – to convert back the sequence of


bytes into the high-level object when accessing the same
data structures.

39

Serialization in Python

Source: https://www.datacamp.com/tutorial/pickle-python-tutorial
40

20
Serialization in Python
Without serialization:
• Have to convert dictionary to a
string when writing string objects
to text files.
• This means that the original state
of the dictionary is lost.
student object is a dictionary type • Hence, the dictionary is printed as
a string, and it will return an error
when trying to access its keys or
values.

Student_info.txt

41

Serialization in Python
With serialization:
When dealing with complex data types
(dictionaries, list, tuple), serialization
allows the user to preserve the object’s
original state without losing any
relevant information.

faculty_file.pkl

42

21
Serialization in Python
Python Pickle
 It is used to serialize and deserialize a Python object
structure.
 It may cause critical security vulnerabilities in code,
hence we should never unpickle data that is untrusted.

43

Serialization in Python
Python Pickle
 Pickling: The process of converting the data structure into a
byte stream before writing to the file.

Data structure Pickling Byte stream

 dump()
 It performs pickling operation on the binary files.
 It returns the object representation in byte mode and store the object
data to the file.
import pickle
pickle.dump(object, file) 44

22
Serialization in Python
Python Pickle
 Unpickling: The process of reverse conversion of byte stream
back to the structure (lists, dictionary, tuples etc.)

Byte stream Unpickling Data structure

 load()
 It reads data from a binary file or file object.
import pickle
pickle.load(file)

45

Serialization in Python

test.dat

46

23

You might also like