Python is a widely popular programming language that is used for a wide variety of applications. Some use cases include automation, data science, machine learning, web development, and much more.
For this project, I created a script that extracted the names and sizes of files in a directory and stored those values in a dictionary.
The script can be found below and also on my Github repository here.
The script was developed by doing the following:
import os
: This imports the OS module which allows the user to interact with the native Operating System Python is running on.file_info ={}
: This created an empty dictionary called file_info. The names and sizes of the files will be stored in this dictionary.current_directory = os.getcwd()
:os.getcwd()
is used to get the current working directory in which the script is being run. That value is then stored in the variable ‘current_directory’.
Please note, that I added two commands on lines 9 and 10 to print the value of the variable and its type. As I am still new to Python, this helps me keep track of the variables I create and helps me stay on the right track. Those lines are not necessary
files = os.listdir(current_directory)
:os.listdir(path)
is used to return a list of all filenames and directories within a given path. In this case, the path is the current working directory stored in the ‘current_directory’ variable. This list is then stored in a variable called ‘files’
Now that a list of filenames and directories has been generated, it is time to iterate through that list and determine the size of ONLY the files. To do this, a for
loop was used to iterate through the filenames in the ‘files’ variable. This can be seen on line 16 of the script.
file_path = current_directory + ‘/’ + file
: This combines the current working directory (stored in the ‘current_directory’ variable) and adds a ‘/’ with the file name to create an absolute path as the variable ‘current_directory’ does not check for the file name. An absolute path specifies the complete location of a file or directory starting from the root directory of the system (e.g. /home/user/documents/testfile.txt). This is stored in a variable ‘file_path’ that will be used later on.if os.path.isfile(file_path) == True:
os.path.isfile(file_path) checks to see if the given path provided is a file. It gives a Boolean response (True or False). Using an if statement, this command checks if a given file_path is a file. If the response is true, it moves on to the next set of commands inside the if statement. This was done to make sure only the sizes of files are gotten and not directories. On lines 29 and 30 of the code, an else statement and pass command are used to skip iteration if it is determined that the provided path is not a file.file_size = os.path.getsize(file_path)
: This command is inside the if statement.os.path.getsize(file_path)
provides the size of a file and returns it in bytes. This value is then stored in the ‘file_size’ variablefile_info[file] = str(file_size) + ‘ Bytes’
: Following the last command, once the file_size is saved, it is converted to a string using the str() command and concatenated to a string ‘Bytes’. This was done to improve readability. The size of the file is then stored in the dictionary created earlier. The key is the name of the file and the value is the size of the file. See the picture below for an example of the outcome.
Thank you for reading thus far.
Please let me know if you have any questions. Happy learning!