- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a directory using Python?
To create a directory, first check if it already exists using os.path.exists(directory). Then you can create it using:
import os if not os.path.exists('my_folder'): os.makedirs('my_folder')
You can also use the python idiom EAFP: Easier to ask for forgiveness than permission. For example,
import os try: os.makedirs('my_folder') except OSError as e: if e.errno != errno.EEXIST: raise
Advertisements