Thursday, January 23, 2025
HomeProgrammingHow to Create New Folder Directory in Python

How to Create New Folder Directory in Python

To create a new folder (directory) in Python, you can use the os or pathlib modules.

  1. Using os.mkdir():
python
import os
os.mkdir('new_folder') # Creates a new folder named 'new_folder'
  1. Using pathlib.Path.mkdir() (Python 3.5+):
python
from pathlib import Path
Path('new_folder').mkdir() # Creates a new folder named 'new_folder'

If you need to create intermediate directories, use os.makedirs() or Path.mkdir(parents=True):

python
os.makedirs('parent_folder/new_folder') # Creates parent folder if needed
# or
Path('parent_folder/new_folder').mkdir(parents=True)

These methods create a directory at the specified path.

See also  Java Logical Operators With Examples
RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x