You are on page 1of 3

Python File System Methods

(https://towardsdatascience.com/10-python-file-system-methods-you-should-know-799f90ef13c2)

The os module is the primary Python module for interacting with the operating system. The shutil module
also contains high-level file operations. For some reason you make directories with os but move and copy
them with shutil.

10 File System Methods


The list below follows this pattern:
Method — Description — Equivalent macOS Shell Command

Get Info
 os.getcwd() — get the current working directory path as a string  — pwd
 os.listdir() — get the contents of the current working directory as a list of strings  — ls
 os.walk("starting_directory_path")— returns a generator with name and path info for
directories and files in the the current directory and all subdirectories— no exact short CLI
equivalent, but ls -R provides subdirectory names and the names of files within subdirectories

Change Things
 os.chdir("/absolute/or/relative/path") — change the current working directory — cd
 os.path.join()—create a path for later use — no short CLI equivalent
 os.makedirs("dir1/dir2") — make directory —mkdir -p
 shutil.copy2("source_file_path", "destination_directory_path") — copy a file
or directory — cp
 shutil.move("source_file_path", "destination_directory_path") — move a file
or directory — mv
 os.remove("my_file_path") — remove a file — rm
 shutil.rmtree("my_directory_path")— remove a directory and all files and directories
in it —rm –rf

Get Info
1. os.getcwd()

os.getcwd() returns the current working directory as a string. That one is straightforward.
2. os.listdir()

os.listdir() returns the contents of the current working directory as a list of strings. That one is also
straightforward.
3. os.walk("my_start_directory")

os.walk() creates a generator that can return information about the current directory and subdirectories. It
works through the directories in the specified starting directory.
os.walk() returns the following items for each directory it traverses:
a. current directory path as a string
b. subdirectory names in the current directory as lists of strings
c. filenames in current directory as a list of strings
It does this for each directory!
It’s often useful to use os.walk() with a for loop to iterate over the contents of a directory and its
subdirectories. For example, the following code will print all files in the directories and subdirectories of
the current working directory.
import os
cwd = os.getcwd()
for dir_path, dir_names, file_names in os.walk(cwd):
for f in file_names:
print(f)

That’s how we get info, now let’s look at commands that change the working directory or move, copy, or
delete parts of the file system.

Change Things
4. os.chdir("/absolute/or/relative/path")

This method changes the current working directory to either the absolute or relative path provided.
If your code then makes other changes to the file system, it’s a good idea to handle any exceptions raised
when using this method with try-except. Otherwise you might be deleting directories or files you don’t
want deleted.
5. os.path.join()

The os.path module has a number of useful methods for common pathname manipulations. You can use it
to find information about directory names and parts of directory names. The module also has methods to
check whether a file or directory exists.
os.path.join() is designed to create a path that will work on most any operating system by joining multiple
strings into one beautiful file path.
Basically, if you are on a Unix or macOS system, os.path.join() sticks a forward slash (“/”) between each
string you provide to create a path. If the operating system needs a “\” instead, then join knows to use a
back slash.
6. os.makedirs("dir1/dir2")

os.makedirs() makes directories. The mkdir() method also makes directories, but it does not make
intermediate directories. So I suggest you use os.makedirs().
7. shutil.copy2("source_file", "destination")

There are many ways to copy files and directories in Python. shutil.copy2() is a good choice because it
tries to preserve as much of the source file’s metadata as possible.
8. shutil.move("source_file", "destination")

Use shutil.move() to change a file’s location. It uses copy2 as the default under the hood.
9. os.remove("my_file_path")

Sometimes you need to remove a file. os.remove() is your tool.


10. shutil.rmtree("my_directory_path")

shutil.rmtree() removes a directory and all files and directories in it.


Note: Careful with functions that delete things! You may want to print what will be deleted as a dry run
with print(). Then run substitute in your remove function for print() when you’re sure it won’t delete the
wrong files.

You might also like