CHAPTER 5
Python Standard
Libraries
Dept. of Computer Engineering
Working with path, files and directories
Python supports file handling and allows
users to handle files.
i.e., to read and write files, along with many
other file handling options, to operate on files.
Python treats file differently as text or
binary and this is
important.
Each line of code includes a sequence of
characters and they form
text file. Each line of a file is terminated with
a special character, called the EOL or End of
Dept. of Computer Engineering
Python standard libraries
Working with paths
Pathlib module in Python provides various
classes representing file system paths with
semantics appropriate for different operating
systems. This module comes under Python’s
standard utility modules.
Path class is a basic building block to work
with files and directories.
import the Path class from pathlib module
from pathlib import Path
Dept. of Computer Engineering
Python standard libraries
Working with paths
can create a path class object like
Path(“C:\\Program Files\Microsoft”)
create a Path object that represents a
current directory like
Path()
can also get the home directory of current
user using home method
Path.home()
Dept. of Computer Engineering
Python standard libraries
Working with paths
create a path object
to get the use of check if the directory
some methods present or not
path = path.is_dir()
Path(“D://testmodule/test.t
xt”) get the file name in
the path by name
check if the path
property
exists or not print(path.name)
path.exist()
get the file name
check if the path without extension by
Dept. of Computer Engineering
Python standard libraries
Working with paths
get the extension
only by suffix
property
print(path.suffix) display the absolute
It will display .py path by using
absolute method.
get the parent
print(path.absolute())
directory by
using parent
property.
print(path.parent)
Dept. of Computer Engineering
Python standard libraries
Working with Directories
Path object can be
created using to iterate through all
path= Path(“test”) the files and
directories
create and remove for p in path.iterdir():
directories print(p)
path.mkdir(‘name’) if we want to see
path.rmdir(‘name’) just the directories,
for p in path.iterdir():
can rename it by if p.is_dir():
path.rename(“abc”) print(p)
Dept. of Computer Engineering
Python standard libraries
Working with files
How we can work with files, refer to the file
to work with using Path class object.
path = Path(“D://testmodule/test.txt”)
check if the file the file using stat
exists or not method
path.exists() print(path.stat())
Reading from a file
rename the file can be done using
using rename read_text method
method print(path.read_text())
path.rename(“init.txt”) can write into the
delete the file using file using write_text
unlink
Dept. of method
Computer Engineering
Python standard libraries
Working with CSV
CSV stands for Comma Separated Values
which is a file that stores the values
separated with comma. They serve as
simple
can usemeans to storefile
csv module and = transfer data.
to work with csv open(“data.csv”,”w”)
files.
import csv csv module has
writer method to
can open a csv file write content into
and write into it by csv file
using built in open writer = csv.writer(file)
function
Dept. of Computer Engineering
Python standard libraries
Working with CSV
Can use writer to write tabular data into csv
file. For this, we need to use write row
method to which we need to pass values in
form of array.
write.writerow([“transaction_id”,”product_id”,”price”])
write.writerow([1000,1,5])
write.writerow([1001,2,15])
file.close()
Here, we have created three rows with three
columns in a csv file.
Dept. of Computer Engineering
Python standard libraries
Working with CSV
Can read the csv file using reader method.
file = open(“data.csv”)
reader = csv.reader(file)
reader = list(reader)
for row in reader:
print(row)
Dept. of Computer Engineering
Python standard libraries
Working with time and datetime
There are two modules which we can use to
work with date and time.
1) time
2) Datetime
Use of time module
import time Output:
time1 = time.time() Wed Nov 23 14:40:52 2022
curr = time.ctime(time1)
print(“current time”,curr)
Dept. of Computer Engineering
Python standard libraries
Working with time and datetime
To work with date and time, we can use
datetime class of datetime module
from datetime import datetime
can create a datetime object
datetime object which represents the
by mentioning year, current date and
month and day. time.
dt = datetime(2022, 12, dt = datetime.now()
25) print(dt)
print(dt)
Output:
Output: 2022-11-23
Dept.2022-12-25
of Computer 00:00:00
Engineering
Python standard libraries
Working with time and datetime
To convert it to datetime object, we need to
specify which part of full date represents
what. So, we can do that by writing:
dt= datetime.strptime("2022/12/25", "%Y/%m/%d")
print(dt)
Output:
2022-12-25 00:00:00
where, %Y, %m and %d are directives to
represent four-digit year, two-digit month and
two-digit date respectively.
Dept. of Computer Engineering
Python standard libraries
Working with time and datetime
can use strftime method to do excat
opposite, i.e. convert datetime object to
string.
dt = datetime(2022,12,25)
print(dt.strftime("%Y/%m/%d"))
Output:
2022/12/25
can convert timestamp into datetime object
using fromtimestamp() method.
import time
Dept.dt=datetime.fromtimestamp(time.time())
of Computer Engineering
Python standard libraries
Working with time and datetime
datetime object has two datetime objects
properties like year to know which date is
and month which we greater
can use to print year
and month. dt1 = datetime(2022,1,1)
print(dt.year) dt2 =
print(dt.month) datetime(2022,12,25)
print(dt2 > dt1)
Output:
2022 above code will print
11 TRUE as date in dt2 is
can also compare greater.
Dept. of Computer Engineering