Dealing with Python Directory Structure: Navigating Files and Folders with OS Modules

Many beginners feel lost when they first learn Python, wondering how to navigate the directory structure and work with files. However, understanding the Python directory structure and using the OS Modulesmakes navigating through files and folders a breeze. In this post, we'll cover the How to navigate files and directories using OS modulesso that you, too, can efficiently handle files and easily manage complex directory structures.

python 디렉토리 포스트 그림

Basics of directory navigation with OS modules

To work with directory structures in Python, use the OS ModulesThe os module is a standard library in Python that allows you to interact with the operating system to handle file and directory-related tasks. Using it, you can easily automate various file and folder operations in your Python programs.

Python 디렉토리 구조 다루기 포스트 그림2

Get a directory listing with os.listdir()

OS module's listdir() function is used to get a list of the files and folders within a directory. It can be used very simply as follows

import os

dir_path = "C:/Users/user/Documents"
file_list = os.listdir(dir_path)
print(file_list)

The above code creates the specified directory path (C:/Users/user/Documents), which returns a list of all the files and folders in the directory. Because it returns only file names, you can do whatever you need to do with them through subsequent processing. It's important to note that listdir() also includes hidden or system files within the current directory.

Securely joining paths with os.path.join()

When combining file or folder paths, rather than typing path separators directly, os.path.join()is safe to use. This function will automatically generate the path using the appropriate delimiter based on your operating system.

import os

base_path = "C:/Users/user/Documents"
file_name = "example.txt"
full_path = os.path.join(base_path, file_name)
print(full_path)

When you run the code above, it will output the path where the file is located and the name/extension of the file. Since different operating systems use different path delimiters, this is a good way to make your code more compatible.

Advanced usage of Python directory structure navigation

In addition to getting files within a directory as a list, OS Modulesallows you to easily navigate through multiple directories and subdirectories. This allows you to quickly find the files you want, even in complex directory structures.

Python 디렉토리 구조 다루기 포스트 그림3

Explore all directories and files with os.walk()

os.walk()is useful for traversing a directory tree and navigating through all files and folders. The function returns three values by default: The path to the current directory, a list of folders within that directory, and a list of files. See the code below:

import os

for dirpath, dirnames, filenames in os.walk("C:/Users/user/Documents"):
    print(f"Current directory path: {dirpath}")
    print(f"List of folders: {dirnames}")
    print(f"List of files: {filenames}")

This code traverses all folders and files under a specified path, giving you an idea of the structure of each directory at a glance, which is especially useful when dealing with complex directory structures.

Filter by file extension

When you have many different kinds of files in a directory, you might want to filter only certain extensions. For example, if you only want to get ".png" files into the list, you could write code like this

import os

dir_path = "C:/Users/user/Documents"
png_files = [file for file in os.listdir(dir_path) if file.endswith(".png")]
print(png_files)

The above code would be called list comprehensionusing the .pngThis way, you can select only the file types you want to process.

Additional useful functions in OS modules

In addition to the OS Modulesprovides many useful functions for handling files. Let's take a look at some of the main ones.

Python 디렉토리 구조 다루기 포스트 그림4

Create directories with os.mkdir() and os.makedirs()

As you work with files, you'll often need to create new directories. os.mkdir() function is used to create a single directory, os.makedirs()can create all the necessary directories at once, including parent directories.

import os

# Create a single directory
os.mkdir("C:/Users/user/Documents/new_folder")

Create a # subdirectory containing
os.makedirs("C:/Users/user/Documents/new_folder/sub_folder")

Deleting files with os.remove()

When you need to delete a file os.remove() function can be used. However, this function can only delete files, not directories.

import os

file_path = "C:/Users/user/Documents/example.txt"
os.remove(file_path)

Deleting directories with os.rmdir() and shutil.rmtree()

To delete a directory os.rmdir()but this function can only delete empty directories. If there are files inside the directory, the shutil.rmtree()should be used.

import os
import shutil

# Delete an empty directory
os.rmdir("C:/Users/user/Documents/new_folder")

Delete directory containing # subfiles
shutil.rmtree("C:/Users/user/Documents/new_folder")

Wrapping Up: A Complete Understanding of Python Directory Navigation

In this post, you learned how to navigate directory structures and manipulate files in Python. Using OS modules, you can easily handle a variety of tasks, from navigating directories to creating and deleting files. These basic Python directory navigation skills will help you take on more complex projects in the future.

Python 디렉토리 구조 다루기 포스트 그림5

If you're a beginner who wants to work efficiently with Python's directory structure, the above methods will make working with files and directories much easier. What can we do after understanding the above? If you're wondering Hereon the bottom of the page!

Similar Posts