Skip to main content

Command Palette

Search for a command to run...

#28 - Python OS Modules

By Ifeanyi Omeata

Published
1 min read
#28 - Python OS Modules
I

Hi, I am a Software Developer of 3-4 years specialising in React, Javascript, Node, NextJS, Express, Python, Django, Fast API, SQL and a few other technology stack, with a good background in Networking and Cloud Infrastructure. I am working to become a DevOps Solutions Engineer and happily married to my long time girlfriend.


Topics:


1. OS Modules


1. OS Modules


>>Return to Menu
The OS module in Python provides functions for creating and removing a directory (folder), fetching its contents, changing and identifying the current directory, etc.




Get Current Working Directory

import os

os.getcwd()

Creating a Directory

import os

os.getcwd()
os.mkdir("MyPythonProject")

Changing the Current Working Directory

import os

os.chdir("C:\MyPythonProject") # changing current workign directory
os.getcwd()

Change CWD to Parent

import os

os.chdir("..")
os.getcwd()

Removing a Directory
We change the current working directory to the parent directory using os.chdir("..") and then remove it using the rmdir() function.

import os

os.chdir("..")
os.rmdir("MyPythonProject")

List Files and Sub-directories
The listdir() function returns the list of all files and directories in the specified directory.

import os

os.listdir()
os.listdir("c:\python37")

#End


Hope you enjoyed this! :) Follow me for more contents...


Get in Touch:
www.ifeanyiomeata.com
contact@ifeanyiomeata.com

Youtube: https://www.youtube.com/c/IfeanyiOmeata
Linkedin: https://www.linkedin.com/in/omeatai/
Twitter: https://twitter.com/iomeata
Github: https://github.com/omeatai/
Stackoverflow: https://stackoverflow.com/users/2689166/omeatai
Hashnode: https://hashnode.com/@omeatai
Medium: https://medium.com/@omeatai
© 2022

LEARN PYTHON/DJANGO

Part 9 of 37

Learn Python and Django the easy way!..... PYTHON | DJANGO | FASTAPI

Up next

#27 - Python File I/O

By Ifeanyi Omeata