0% found this document useful (0 votes)
15 views6 pages

PPL - Assignment No 9

The assignment focuses on understanding Python's os and time modules by writing a program to list directory contents sorted by creation date. It includes explanations of relevant functions, example code, and expected output. Upon completion, students will gain practical experience with these modules and their applications in file management.

Uploaded by

digiworksoft2024
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)
15 views6 pages

PPL - Assignment No 9

The assignment focuses on understanding Python's os and time modules by writing a program to list directory contents sorted by creation date. It includes explanations of relevant functions, example code, and expected output. Upon completion, students will gain practical experience with these modules and their applications in file management.

Uploaded by

digiworksoft2024
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/ 6

Assignment no.

09

Title: Python system modules

Objectives:

• To understand Python modules os and time.

Problem Statement:

Write a Python program to get a directory listing, sorted by creation date.

Outcomes:

On completion of this assignment students will be able to-

• Understand the Python modules os and time

Software & Hardware requirements:

•Open Source Python Programming


tools like Jupyter Notebook.
Theory- Concept:

Python OS Module Functions


Here we will discuss some important functions of the Python os module:
 Handling the Current Working Directory
 Creating a Directory
 Listing out Files and Directories with Python
 Deleting Directory or Files using Python

Example: This code uses the ‘os' module to get and print the current working directory (CWD) of the
Python script. It retrieves the CWD using the ‘os.getcwd()' and then prints it to the console.
import os
cwd = os.getcwd()
print("Current working directory:", cwd)

Importing time module


The time module comes with Python’s standard utility module, so there is no need to install it externally. We
can simply import it using the import statement.
import time

import time
print(time.gmtime(0))

Program Codes with output:


# Import the required modules: 'os' for operating system interactions and 'time' for time-related functions.
import os
import time
# Create a list 'paths' by iterating through files in the current directory and formatting their creation time and
file name.
# The list comprehension generates tuples with the creation time and file name for each file.

paths = ["%s %s" % (time.ctime(t), f) for t, f in sorted([(os.path.getctime(x), x) for x in os.listdir(".")])]

# Print a header for the directory listing.


print("Directory listing, sorted by creation date:")

# Iterate through the list 'paths' and print each entry.


for x in range(len(paths)):
print(paths[x],)

Output:
Directory listing, sorted by creation date:
Sun Apr 16 00:07:24 2023 Downloads
Sun Apr 16 00:07:24 2023 Favorites
Sun Apr 16 00:07:24 2023 Links
Sun Apr 16 00:07:24 2023 Music
Sun Apr 16 00:07:24 2023 Pictures
Sun Apr 16 00:07:24 2023 Saved Games
Sun Apr 16 00:07:24 2023 Videos
Sun Apr 16 00:07:24 2023 Documents
Sun Apr 16 00:07:24 2023 My Documents
Sun Apr 16 00:07:24 2023 Desktop
Sun Apr 16 00:07:24 2023 Start Menu
Sun Apr 16 00:07:24 2023 Recent
Sun Apr 16 00:07:24 2023 SendTo
Sun Apr 16 00:07:24 2023 Cookies
Sun Apr 16 00:07:30 2023 Contacts
Sun Apr 16 00:07:30 2023 Searches
Sun Apr 16 00:09:33 2023 OneDrive
Thu Sep 14 09:02:37 2023 .continuum
Thu Sep 14 09:02:45 2023 .conda
Thu Sep 14 09:02:51 2023 .condarc
Thu Sep 14 09:04:47 2023 .ipython
Thu Sep 14 09:05:35 2023 sam_folder.ipynb
Thu Sep 14 09:05:35 2023 .ipynb_checkpoints
Thu Sep 14 15:14:19 2023 Untitled8.ipynb
Wed Sep 20 16:16:45 2023 AppData
Wed Sep 20 16:16:45 2023 Application Data
Wed Sep 20 16:16:45 2023 NTUSER.DAT
Wed Sep 20 16:16:45 2023 NetHood
Wed Sep 20 16:16:45 2023 PrintHood
Wed Sep 20 16:16:45 2023 Templates
Wed Sep 20 16:16:45 2023 Local Settings
Wed Sep 20 16:16:45 2023 NTUSER.DAT{4a7af8e0-57ab-11ee-9d72-e0be037c5d24}.TM.blf
Wed Sep 20 16:16:45 2023 NTUSER.DAT{4a7af8e0-57ab-11ee-9d72-
e0be037c5d24}.TMContainer00000000000000000001.regtrans-ms
Wed Sep 20 16:16:45 2023 ntuser.dat.LOG1
Wed Sep 20 16:16:45 2023 ntuser.dat.LOG2
Mon Oct 9 13:02:02 2023 Untitled.ipynb
Mon Oct 9 13:03:29 2023 Untitled9.ipynb
Fri Oct 13 10:38:14 2023 calc.py
Fri Oct 13 10:40:11 2023 pycache
Fri Oct 13 10:41:12 2023 Untitled10.ipynb
Sat Nov 25 10:20:06 2023 Untitled11.ipynb

Conclusion:
Hence we have learned the Python modules os and time

Assignment Questions: (Write the Answer of following question in Your Assignment book and Get it
checked)
Q. What are the Python modules?

You might also like