When using a PC, managing files and folders is a common task, so why not use Python to handle it? (Document)
No matter how large your codebase becomes, working with files and folders is inevitable. I’m writing this post because I plan to minimize its use in future posts, and I want to provide a reference for you in case I don’t cover it later. For Python 3.3 and newer, the pathlib
module is a great choice for this purpose.
File
- Purpose: Files are used to record and read data for various tasks, such as writing text, storing code, or playing music and videos.
- Definition: A file is the fundamental unit for storing data, available in various forms like documents, images, and videos.
- Structure: Each file is identified by a name and an extension and is stored in a format readable and writable by specific programs.
Folder
- Use for: Folders help organize and categorize files, making them easier to manage and essential for structuring projects or workflows.
- Definition: A folder is a storage space used to group and organize files.
- Structure: Folders can contain files and other folders (subfolders), forming a hierarchical structure for easy navigation and organization.
File & Folder Operation Code
import os
# get os environ
print(os.environ['PATH'])
# get current path
print(os.getcwd())
# get upper path
os.chdir('..//')
print(os.getcwd())
# get current folder list
os.chdir(os.path.dirname(os.path.abspath(__file__)))
print(os.listdir())
# Create and Delete file folder
if not os.path.isdir('temp'):
os.mkdir('temp')
print(os.listdir())
os.chdir('./temp')
print(os.getcwd())
os.chdir('..//')
print(os.getcwd())
os.removedirs('temp')
print(os.listdir())
# Only current fold name without path
curr_path = os.path.dirname(os.path.abspath(__file__))
print(os.path.basename(curr_path))
# Current fold path
print(os.path.dirname(curr_path))
# Split Path + Current Fold
print(os.path.split(curr_path))
# Check exist path
print(os.path.exists(curr_path))
Here are some essential os
module commands in Python for working with files and folders:
os.environ['PATH']
: Retrieves the environment variables from the environment Python is running in.os.getcwd()
: Gets the current working directory.os.chdir('folder')
: Changes the current working directory.os.mkdir('folder')
: Creates a new folder.os.removedirs('folder')
: Deletes a folder.os.remove('file')
: Deletes a specific file.
These commands are useful in various scenarios, such as loading external files or saving specific data. I won’t delve into file creation or writing here since they are relatively straightforward and less engaging.
On a side note, I recall how innovative and utilitarian I found the Mac’s file tagging system compared to folder-based organization. However, even after 20 years of learning about it, I still rely on folders and rarely use tags, which seems to be the case for many others as well.
Today, libraries like pathlib
are often recommended over the os
module for their improved functionality and ease of use. When I get the chance, I’ll write a separate post about pathlib
and its features.