Wednesday, January 8, 2025
HomeProgrammingHow to create new folder? - python

How to create new folder? [duplicate] – python

To create a new folder in Python, use the os or pathlib modules:

Using os:

python
import os

folder_name = "new_folder"
os.makedirs(folder_name, exist_ok=True) # Creates the folder if it doesn't exist

Using pathlib:

python
from pathlib import Path

folder_name = "new_folder"
Path(folder_name).mkdir(parents=True, exist_ok=True) # Creates the folder if it doesn't exist

Key Points:

  • Replace "new_folder" with your desired folder name or path.
  • exist_ok=True avoids errors if the folder already exists.
  • Use parents=True to create intermediate directories if needed.
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