Program to display current working directory and list of files

This blog discuss how to use os module for printing current working directory along with list of files in current directory

Require to import os module in order to know current working directory and to display list of files

Use function os.getcwd() to display current working directory

Use function os.listdir() to display list of files in current directory or in a path specified

os.listdir()
Return a list of files in the directory specified by a path
If no path is specified, it display files from current working directory
It display files in arbitrary order

Below is the complete code to display current working directory and list of files

import os
print('Current Working Directory:',os.getcwd())
print('Below is the list files')
print((os.listdir()))

Output

Current Working Directory: C:\Users\rohit\PycharmProjects\pythonProject
Below is the list files
['.idea', 'main.py', 'venv']
Advertisement

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s