Mod 4
Mod 4
Python Programming
Module 4
• Course Objectives:
• CO1004.1: Students will be able to explain the fundamental components of programming languages, including syntax, semantics, data types, and control structures.
• CO1004.2: Students will enhance their logical thinking and creativity by solving a range of programming challenges and exercises.
• CO1004.3: Students will create their own functions and handle the exceptions
• CO1004.4: Students will be able to efficiently manage files, perform read/write operations
• CO1004.5: Students will be able to create and manage GUI applications, understand components and events, and utilize various widgets like buttons, entries, texts,
and check buttons effectively.
File handling Modes, Reading Files, Writing & Appending to Files, Handling File Exceptions
The with statement. Examples of use of Python libraries like numpy, pandas and matplotlib in
File handling
Computational
thinking with Python Hacker rank
1 Programming Dr. Priya Charles Quiz Project pgmming
Binary files are faster and easier for a program to read and write than
text files.
Data in binary files cannot be directly read, it can be read only through
python program for the same.
1. Open the file and associate the file with a file variable.
2. A command to read the information.
3. A command to close the file.
1. OPENING FILE
We should first open the file for read or write by specifying the name of file and
mode.
2. PERFORMING READ/WRITE
Once the file is opened now we can either read or write for which file is opened
using various functions available
3. CLOSING FILE
After performing operation we must close the file and release the file for other
application to use it,
SYNTAX:
file_object = open(filename)
Or
file_object = open(filename,mode)
** default mode is
“read”
myfile = open(“story.txt”)
here disk file “story.txt” is loaded in RAM
myfile = open(“article.txt”,”r”)
here “r” is for read (although it is by default, other
options are “w” for write, “a” for append)
myfile = open(“d:\\mydata\\poem.txt”,”r”)
here we are accessing “poem.txt” file stored in
separate location i.e. d:\mydata folder.
at the time of giving path of file we must use double backslash(\\) in place of single
backslash because in python single slash is used for escape character and it may cause
problem like if the folder name is “nitin” and we provide path as d:\nitin\poem.txt then
in \nitin “\n” will become escape character for new line, SO ALWAYS USE DOUBLE
BACKSLASH IN PATH
B
:
myfile = open(“d:\\mydata\\poem.txt”,”r”)
here we are accessing “poem.txt” file stored in separate location i.e. d:\mydata
folder.
at the time of giving path of file we must use double backslash(\\) in place of single
backslash because in python single slash is used for escape character and it may cause
problem like if the folder name is “nitin” and we provide path as d:\nitin\poem.txt then in
\nitin “\n” will become escape character for new line, SO ALWAYS USE DOUBLE
BACKSLASH IN PATH
myfile = open(“d:\\mydata\\poem.txt”,”r”)
myfile = open(r“d:\mydata\poem.txt”,”r”)
myfile.close()
Note: open function is built-in function used standalone while close() must
be called through file handle
RAKA
SHAKAAL
GABBAR
Lets run
the same
program
again
D Y Patil International University School of Engineering
Example-1:
write() using “w”
mode
Now we can observe that while writing data to file using “w”
mode the previous content of existing file will be overwritten and
new content will be saved.
New content is
added after
previous
content
D Y Patil International University School of Engineering
Example-3:
using
writelines()
Now content is
stored, because of
close() function
contents are flushed
pushed in
D Y Patil International University file School of Engineering
Example: working of flush()
With flush() All
contents
before
flush() are
present in
file