Python mkdir
To create a directory (folder) in Python, you can use the os.mkdir()
function from the os
module. Here’s an example:
import os
# Specify the directory path
directory = '/path/to/directory'
# Create the directory
os.mkdir(directory)
Replace '/path/to/directory'
with the actual path where you want to create the directory. Note that the path should be a valid path on your system.
If the directory already exists or there is an error in creating the directory, os.mkdir()
will raise an exception. To handle such cases, you can use a try-except
block like this:
import os
directory = '/path/to/directory'
try:
os.mkdir(directory)
print("Directory created successfully.")
except OSError as error:
print(f"Failed to create directory: {error}")
This way, you can catch the exception and display an appropriate error message if the directory creation fails.
Python Training Demo Day 1
Conclusion:
Unogeeks is the No.1 IT Training Institute for Python Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Python here – Python Blogs
You can check out our Best In Class Python Training Details here – Python Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks