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:
ifeanyiomeata.com
contact@ifeanyiomeata.com
Youtube: youtube.com/c/IfeanyiOmeata
Linkedin: linkedin.com/in/omeatai
Twitter: twitter.com/iomeata
Github: github.com/omeatai
Stackoverflow: stackoverflow.com/users/2689166/omeatai
Hashnode: hashnode.com/@omeatai
Medium: medium.com/@omeatai
© 2022