Disclosure: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.
In the software industry, most of the programs handle files in one way or another, such as creating files, renaming them, moving, listing, etc. As a programmer, you should definitely have this fundamental skill. As a result, in this tutorial, we will be using os module in Python to make several operations on files and directories regardless of the operating system we're using.
Now, you need to know that os module isn't only for handling files and directories, it has a ton of methods and tools for other operations, such as handling environment variables, command-line arguments, system process management, and even Linux extended attributes that are specific for Linux operating system.
Alright, let's get started, since os module is a built-in module, we don't have to install anything.
This may also interest you: How to Get the Size of Directories in Python.
In this tutorial, you will learn:
In order to get the current working directory, you need to use os.getcwd():
import os # print the current directory print("The current directory:", os.getcwd())
os.getcwd() returns a Unicode string representing the current working directory, here is my output:
The current directory: C:\pythoncode-tutorials\python-standard-library
To make a folder/directory in any operating system, all you need to do is:
# make an empty directory (folder) os.mkdir("folder")
Once you execute this, it'll immediately spawn a directory named "folder" in the current working directory.
If you run this again, it will raise a FileExistsError, that's because the folder already exists, a good way to solve this, is to run os.mkdir() only if the folder doesn't exist, we can easily do this:
# running mkdir again with the same name raises FileExistsError, run this instead: if not os.path.isdir("folder"): os.mkdir("folder")
os.path.isdir() function returns True if the pathname we passed refers to an existing directory.
It is fairly easy to change directories, let's change to the folder we just created:
# changing the current directory to 'folder' os.chdir("folder")
Now let's print the working directory again:
# printing the current directory now print("The current directory changing the directory to folder:", os.getcwd())
The current directory changing the directory to folder: C:\pythoncode-tutorials\python-standard-library\folder
Let's say you want to create not only one folder, but you want to create many nested folders:
# go back a directory os.chdir("..") # make several nested directories os.makedirs("nested1/nested2/nested3")
This will create 3 folders recursively as shown in the following image:
In order to create files in Python, you don't need any module, you can use the built-in function open() which takes the filename you want to create as the first parameter and the mode you want to open the file with as a second parameter:
# create a new text file text_file = open("text.txt", "w") # write to this file some text text_file.write("This is a text file")
I used "w" as file opening mode which stands for write, you can also use "a" for appending to existing files or "r" for reading existing files. For more information about file opening modes, check this page.
It's pretty easy to rename a file using the os module, let's rename the file we just created:
# rename text.txt to renamed-text.txt os.rename("text.txt", "renamed-text.txt")
os.rename() function takes 2 parameters, the name of file or directory you want to rename, and the destination name you want to rename to.
We can use os.replace() function to move files or directories:
# replace (move) this file to another directory os.replace("renamed-text.txt", "folder/renamed-text.txt")
Note that this will overwrite the destination, so if there is another pre-existing file in "folder" that got the same name "renamed-text.txt", but different content, it will overwrite it.
# print all files and folders in the current directory print("All folders & files:", os.listdir())
os.listdir() function returns a list containing the names of the files in the directory, we passed nothing as a parameter, so it'll return the files and directories of the current working directory, here is my output:
All folders & files: ['folder', 'handling-files', 'nested1', 'text.txt']
Okay, but what if we want to know the content of these folders too? We gonna need to use the os.walk() function instead:
# print all files & folders recursively for dirpath, dirnames, filenames in os.walk("."): # iterate over directories for dirname in dirnames: print("Directory:", os.path.join(dirpath, dirname)) # iterate over files for filename in filenames: print("File:", os.path.join(dirpath, filename))
os.walk() is a directory tree generator, it will iterate over all the directory trees, I've passed "." as the top of this tree, which refers to the current working directory, here is the output:
Directory: .\folder Directory: .\handling-files Directory: .\nested1 File: .\text.txt File: .\handling-files\listing_files.py File: .\handling-files\README.md Directory: .\nested1\nested2 Directory: .\nested1\nested2\nested3
I've also used os.path.join() method to join the current path we're in, with the file/directory name.
Let's remove that file we created:
# delete that file os.remove("folder/renamed-text.txt")
os.remove() does what its name suggests, it removes a file (not a directory) given its pathname. Alternatively, you can use
os.unlink() to do the exact same thing.
Using os.rmdir() function will delete a given folder:
# remove the folder os.rmdir("folder")
To delete directories recursively, we need to use os.removedirs() function instead:
# remove nested folders os.removedirs("nested1/nested2/nested3")
This will remove all these directories only if they're empty of course.
In order to delete non-empty directories (directories that has files, and subdirectories as well), we need to use
rmtree() method from shutil:
# remote non-empty folders shutil.rmtree("nested1")
This method will recursively delete the entire
nested1 directory tree, of course, you'll need the necessary privileges to do so, as it's quite dangerous if you're unconscious about what you're doing.
In order to get some information about a given file in your operating system, there is a cool function os.stat() which performs a stat system call on the given path:
open("text.txt", "w").write("This is a text file") # print some stats about the file print(os.stat("text.txt"))
Here is the output:
os.stat_result(st_mode=33206, st_ino=14355223812608232, st_dev=1558443184, st_nlink=1, st_uid=0, st_gid=0, st_size=19, st_atime=1575967618, st_mtime=1575967618, st_ctime=1575966941)
This returned a named tuple that got some metrics on it, we won't cover all these attributes, but some are interesting:
st_size: The size of the file in bytes.
st_atime: Time of most recent access expressed in seconds (timestamp).
st_mtime: Time of most recent modification.
st_ctime: In Windows, this is the time of the creation of the file, in Unix, this is the time of the most recent metadata change.
To get a specific attribute, you can do as follows:
# get the file size for example print("File size:", os.stat("text.txt").st_size)
File size: 19
Check this page for further information about these attributes.
As you can see, it is very easy to handle files and directories in Python using this platform-independent module, in which you don't need to worry about any platform-specific handling, etc. You can also use some platform-specific functions, such as os.chown() or os.chmod() for Linux.
Of course, we didn't cover everything, please read Python's official documentation for further details.
Finally, if you're a beginner and want to learn Python, I suggest you take the Python For Everybody Coursera course, in which you'll learn a lot about Python, good luck!
Learn also: How to Download Files in Python.
Happy Coding ♥View Full Code