making directory in python code example
Example 1: python create directory
import os
os.mkdir('tempDir')
os.mkdir('tempDir2/temp2/temp')
os.makedirs('tempDir2/temp2/temp')
Example 2: create folder python
import os
path = "/tmp/year"
try:
os.mkdir(path)
except OSError:
print ("Creation of the directory %s failed" % path)
else:
print ("Successfully created the directory %s " % path)
Example 3: create directory in python
import os
directory = "Krishna"
path_dir = "C:/Users/../Desktop/current_dir/"
if not os.path.exists(directory):
os.mkdir(os.path.join(path_dir, directory))
Example 4: how to add directory to python script
from pathlib import Path
basepath = Path('my_directory/')
files_in_basepath = basepath.iterdir()
for item in files_in_basepath:
if item.is_file():
print(item.name)