Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

OS dir (mkdir, makedirs, remove, rmdir)

  • mkdir

  • makedirs

  • remove

  • unlink

  • rmdir

  • removedirs

  • rmtree

  • shutil

  • mkdir is like mkdir in Linux and Windows

  • makedirs is like mkdir -p in Linux

  • remove and unlink are like rm -f in Linux or del in Windows

  • rmdir is like rmdir

import os
import shutil

# create a single directory
path_to_new_dir = 'abc'
os.mkdir(path_to_new_dir)

# create also the parent directories, if needed
path_to_new_dir = 'dir/subdir/subdir'
# os.mkdir(path_to_new_dir) # will fail if 'dir' or 'dir/subdir' does not exist
os.makedirs(path_to_new_dir)


#  remove a file (both)
os.remove(path_to_file)
os.unlink(path_to_file)

# remove single empty directory
os.rmdir(path_to_dir)

# remove directory tree if there are no files in them
os.removedirs(path_to_dir)

# Remove a whole directory structure (subdirs and files)
# Like rm -rf
shutil.rmtree(path_to_dir)