In this article, we will learn about Python files and directories management. We often come across use cases wherein we want to some or other operation on files and directories separately. For such use cases, we first have to segregate files and directories and then operate on them separately. For example, let’s say we want to list down only the directories in a specific path in Linux and traverse each directory. To do so, we can first separate the files and directories and store them in a dictionary data structure. Later store the dictionary key values where directories are placed in a new list We will then apply the logic to traverse each directory in this new list.
Python OS and JSON Module Overview
OS Module
OS module in Python provides a variety of functions to interact with the operating system and perform various system related operations. OS module is used to perform various operations on files and directories, environment variables, gather information about the operating system and so on. It is also used to interact with running process in the system or creating new processes.
JSON Module
JSON module in python is mostly used for encoding and decoding of JSON data. JSON dumps function is used to convert Python objects (list, string, dictionary etc.) into JSON representation. JSON module is also used to read and write data from and to JSON files. it also supports error handling classes that deals with JSON related errors.
Prerequisite
- Linux Operating System
- Python3 Installed
- IDE (Jupyter notebook, VS Code etc)
Python Files and Directories Managment
Also read: [Solved] bash: ifconfig: command not found
Below is the Python code which can be used to segregate Files and Directories from a given path in Linux and store them in a dictionary. Save the code in a file called vehicle.py.
import os import json PATH='/root/vehicle/python' LIST_PATH = os.listdir(PATH) SEPARATE_LIST = {"file": [], "directory": []} DIR_PATHS = [os.path.join(PATH, item) for item in LIST_PATH] def segregateCWD(): for path in DIR_PATHS: if os.path.isdir(path): SEPARATE_LIST['directory'].append(path) if os.path.isfile(path): SEPARATE_LIST['file'].append(path) print(json.dumps(SEPARATE_LIST, indent=4, sort_keys=True)) print(f"Total number of Files and Directories in path {PATH}: {len(LIST_PATH)}") print(f"Total number of files in path {PATH}: {len(SEPARATE_LIST['file'])}") print(f"Total number of files in path {PATH}: {len(SEPARATE_LIST['directory'])}") segregateCWD()
Let’s understand what each statement in the above code is doing.
OUTPUT
[root@linuxnasa vehicle]# python3 vehicle.py { "directory": [ "/root/vehicle/car", "/root/vehicle/bike", "/root/vehicle/bus" ], "file": [ "/root/vehicle/Readme.txt", "/root/vehicle/file1.txt", "/root/vehicle/file2.txt", "/root/vehicle/file3.txt" ] } Total number of Files and Directories in path /root/vehicle: 7 Total number of files in path /root/vehicle: 4 Total number of files in path /root/vehicle: 3
Summary
This is the simplest approach to segregate and the files and directories in Python. There are lot many other way to do the same job. Example, we can also use os.scansir() to do the segregation. We can also store the segregated files and directories to two different directories instead of lists.