make folder and go to it in python

To create a folder and navigate to it in Python, you can use the os module.

Here's the code to achieve this:

main.py
import os

# Specify the folder name
folder_name = "myfolder"

# Create the folder
os.mkdir(folder_name)

# Change the working directory to the created folder
os.chdir(folder_name)

# Get the current path
current_path = os.getcwd()
print("Current path:", current_path)
269 chars
15 lines

The os.mkdir(folder_name) function creates a folder with the specified name. The os.chdir(folder_name) function changes the working directory to the newly created folder. Finally, os.getcwd() returns the current directory path, which should be the path to the created folder.

Make sure to import the os module at the beginning of your code.

related categories

gistlibby LogSnag