We will show some of the many things that we can do with files in Python. For these operations, we’ll be using functions provided by the os module which acts as a layer of abstraction between Python and the operating system. It allows us to interact with the underlying system without us knowing whether we’re working on a Windows, Mac or Linux. This means that you can write and test a script on one operating system like Windows and then run it on a different operating system like Linux. But one thing to watch out for, paths can be different across different operating systems. So whenever we’re using an absolute path in our code, we need to make sure we can provide alternatives for the platforms we want to support. The os module lets us do pretty much all the same tasks that we can normally do when working with files from the command line. We can change the file permissions and delete or rename files through our code. This means you can write scripts to do these operations for you automatically.
At this point, we will provide some examples of how you can remove or rename a file as well as to check if this file exists.
import os # how to delete a file os.remove("myfile.txt") # how to rename a file os.rename("first_filename.txt", "final_file.txt") # how to check if the file exists. It returns either True or False os.path.exists("myfile.txt")
Let’s provide some other examples of how you can get the size of the file, the last modified date and how you can convert Unix Timestamps to DateTime format
import os # how to get the size of the file os.path.getsize("myfile.txt") # 56 # how to check when was the last time # that this file modified. It returns the time in Unix Timestamp os.path.getmtime("myfile.txt") # 1586518027.4692106 # Return the Unix Timestamp to Date Time import datetime mytimestamp = os.path.getmtime("myfile.txt") datetime.datetime.fromtimestamp(mytimestamp) # datetime.datetime(2020, 4, 10, 14, 27, 7, 469211) # How to check if the input is a file os.path.isfile("myfile.txt") # True os.path.isfile("Music") # False
In case we want to get more details about a file then we can use the os.stat() function which provides statistics of the file such as the st_size (size of the file), the st_atime (most recent access of the file), the st_mtime (the last modification time) and the st_ctime (creation time).
os.stat("myfile.txt") # os.stat_result(st_mode=33206, st_ino=9851624185481238, st_dev=2493502220, st_nlink=1, st_uid=0, st_gid=0, st_size=56, # st_atime=1586518027, st_mtime=1586518027, st_ctime=1586517986) # in case we weant to retrieve a specific information, like the creation time: os.stat("myfile.txt").st_ctime # 1586517986.6047087
Another cool feature of the functions is that we can work with both relative and absolute paths. In our examples, we’ve been using the relative file names without having to specify their full paths. In some cases, we may need to specify exactly where the file is to work with it in our script. This is where the abspath function can help.
import os # get the absolute path os.path.abspath("myfile.txt") # 'C:\\Users\\gpipis\\myfile.txt'
We will provide some examples of how you can get the working directory, to change directory, to remove a directory and to list all the files of the directory
import os # How to get your current working directory print(os.getcwd()) # C:\Users\gpipis # How to create a directory os.mkdir("new_dir") # How to change a directory os.chdir("new_dir") print(os.getcwd()) # C:\Users\gpipis\new_dir # How to remove an EMPTY directory os.rmdir("new_dir") # How to list all the files/directories of the directories os.listdir("Documents") # How you can get the full path os.path.join("Documents", "My Pictures") # 'Documents\\My Pictures' # How to check if the file is a directory - Note it needs the full path os.path.isdir(os.path.join("Documents", "My Pictures")) # True os.path.isdir(os.path.join("Documents", "mygzfile.tar.gz")) # False
We can use the subprocess module if we want to run a system command as part of our Python script to accomplish a task, or use some functionality that doesn’t exist in the Python modules, neither built-in or external.
Let’s provide some examples. Notice the returcode=0 means a success, where the returncode=2 means an error.
# Example of a "Hello World" subprocess.check_output(['echo', 'hello world']) #b'hello world\n' # How to get the date import subprocess subprocess.run(["date"]) # Fri Apr 10 15:18:20 GTBDT 2020 # CompletedProcess(args=['date'], returncode=0) # How to get the list of files subprocess.run(["ls"])