5,323
edits
Line 130: | Line 130: | ||
==== | ====Directories==== | ||
Create, Move, and Delete directories or folders | |||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
import os, shutil, time | import os, shutil, time | ||
import os.path as path | import os.path as path | ||
# | # Create a directory | ||
os.makedirs("new_dir", exist_ok=True) | |||
# or os.makedirs(os.path.dirname("new_dir/my_file.txt"), exist_ok=True) | |||
# Delete an empty directory | # Delete an empty directory | ||
os.rmdir(dir_path) | os.rmdir(dir_path) | ||
# Delete an empty or non-empty directory | # Delete an empty or non-empty directory | ||
shutil.rmtree(dir_path) | shutil.rmtree(dir_path) |