-
Python樹林
How to Get File Extensions in Python Using os.path.splitext
In file manipulation programs, it is often necessary to branch processing based on file extensions. Common scenarios include "processing only image files (.jpg, .png)" or "excluding anything other than text files (.txt)". The Python stan... -
Python樹林
How to Check if a Path is a File or Directory in Python: Using os.path.isfile and os.path.isdir
In file manipulation programs, you often need to distinguish whether a specified path is a "file" or a "directory (folder)" to handle them differently. For example, after getting a list of items in a directory, you might want to perform ... -
Python樹林
How to List Files in a Directory in Python: Using pathlib.iterdir() vs os.listdir()
In file manipulation scripts, you often need to process all files inside a specific folder. There are two main ways to get a list of contents in a directory in Python: pathlib module (Recommended): An object-oriented, modern approach int... -
Python樹林
How to Check File and Directory Existence in Python Using os.path
Before performing file operations, checking "if the file is really there" is very important to prevent errors like FileNotFoundError. The Python standard library os.path module provides several functions to check for existence. You need ... -
Python樹林
Getting and Converting Absolute and Relative Paths in Python: Usage of os.path.abspath and relpath
In programs that perform file operations, we use a "Path" to specify the file location. There are two types of paths: "Absolute Path," which describes the location from the root directory, and "Relative Path," which describes the locatio... -
Python樹林
How to Get and Change the Current Directory in Python: Using os.getcwd and os.chdir
When reading or writing files in a Python script using relative paths (e.g., data.txt), the program searches for them based on the "Current Directory (Current Working Directory)." Therefore, knowing where your program is currently runnin... -
Python樹林
Separating File Name and Directory from a Path in Python: How to Use os.path.split
In programs handling file paths, operations like "extracting just the filename from a full path" or "getting the parent directory path excluding the filename" are frequent. Python's standard library os.path module provides the split() fu... -
Python樹林
Joining Paths in Python: Usage of os.path.join and the Reset Behavior with Absolute Paths
When handling file or directory paths in a program, concatenating directory names and file names to create a single path string is a frequent operation. While it is possible to simply add strings (+) or use the join method with /, this c... -
Python樹林
Getting Path Separators in Python: Usage of os.sep and Cross-Platform Compatibility
When trying to run Python scripts on both Windows and macOS (or Linux), the difference in "file path separators" often becomes a problem. Windows: Backslash \ (written as \\ in Python strings) macOS / Linux: Forward slash / If you hardco... -
Python樹林
Writing to Text Files in Python: Differences Between write() and writelines(), and Handling Newlines
When saving calculation results or log data to a file in Python, you open the file in "write mode" using the built-in function open() and use methods to write the data. There are two methods for writing: write(), which writes a single st... -
Python樹林
Reading Text Files in Python: Differences and Usage of read, readline, and readlines
When reading the contents of a text file in Python, you use the built-in open() function. There are three main methods for "extracting data" afterwards: read(): Reads the entire content as a single string. readlines(): Reads the entire c... -
Python樹林
Python File Operations: List of open() Modes and Safe Reading/Writing with with Statement
When dealing with text or binary files in Python, use the built-in function open(). This function creates a file object by specifying a "mode" such as reading, writing, or appending. Also, in file operations, you must close the file (rel... -
Python樹林
Measuring Memory Usage in Python: How to Use the tracemalloc Module to Identify Bottlenecks
While Python is a language that handles memory management automatically (it has garbage collection), excessive memory usage can become a problem in processes handling large data or long-running server applications. In such cases, you can... -
Python樹林
Python Execution Time Measurement: Difference and Usage of time.perf_counter() and time.process_time()
When evaluating program performance, accurately measuring "how long a process took" is crucial. Python's standard library time module provides several functions for measuring time, but you need to select the appropriate one depending on ... -
Python樹林
Python Type Hints: Union (|) for Allowing Multiple Types and Any for Accepting Any Type
When using Python type hints, you encounter cases where "this variable might contain an integer or a string" or "the data type cannot be specified." To handle such flexible type definitions, Python provides the Union type (or | operator)... -
Python樹林
Python Type Hints: How to Specify Types Inside Lists (list) and Dictionaries (dict)
When defining collection variables like lists (list) or dictionaries (dict) in Python, simply writing items: list is often not enough. Without information like "I know it's a list, but does it contain numbers or strings?", there is a ris... -
Python樹林
Introduction to Python Type Hints: How to Add Type Information to Variables and Functions for Better Readability
Python is a "dynamically typed language," meaning you can write code without declaring variable types (integer, string, etc.). While this flexibility is appealing, it can cause confusion in large-scale or team development, such as "What ... -
Python樹林
Creating High-Performance Command-Line Tools in Python: How to Use the argparse Module
Unlike sys.argv, which simply receives command-line arguments as a list, using Python's standard library argparse allows you to easily implement advanced features such as "automatic help message generation," "argument type checking," and... -
Python樹林
Improving Python Code: Learning “Pythonic” Writing from Anti-Patterns
Python has a "recommended way of writing" called "Pythonic," which leverages features and conventions unique to Python. Bringing habits from other programming languages or continuing to write verbose code is called an "Anti-pattern," whi... -
Python樹林
Python Coding Standards: Basic Rules of PEP and PEP 8 (Style Guide)
Python is a language designed with a focus on "readability." To ensure that code looks consistent regardless of who writes it, the Python community follows a standard coding convention called "PEP 8". This article explains what PEP is an... -
Python樹林
Reading INI Configuration Files in Python: Usage of configparser’s get, getint, and getboolean
Separating configuration information (such as database connection details, debug mode flags, and timeout settings) into external files is a fundamental practice in software development. Python provides a standard library called configpar... -
Python樹林
Python unittest Execution Guide: Running Tests Collectively or Individually from the Command Line
Test code created with unittest can be executed not only directly as a Python script (writing if __name__ == "__main__":), but also more flexibly using the python -m unittest command from the command line. Using this command allows you t... -
Python樹林
Python unittest Setup and Teardown: Complete Guide to setUp, tearDown, setUpClass, and tearDownClass
When performing unit tests, you often need "pre-processing" such as connecting to a database or preparing initial data before running tests. You also need "post-processing" to delete temporary files or close connections after tests finis... -
Python樹林
Introduction to Python’s unittest: How to Write Unit Tests with the Standard Library and a List of Assertion Methods
To maintain high-quality code, "testing" is essential to verify that the functions and classes you create work as intended. Python comes with the unittest module as a standard library, allowing you to perform full-scale unit testing with... -
Python樹林
Outputting Logs to Both File and Console with Python logging: Using Handlers
Log information is crucial when developing and operating applications. While you want to check operations in real-time on the console (screen) during development, you also need to permanently save logs as "files" for production operation... -
Python樹林
Python logging: Customizing Log Output Formats and Variable List
Simply displaying an error message in logs is often insufficient. Logs become useful for debugging and monitoring only when they contain metadata such as "when," "in which file," and "on which line" the event occurred. In Python's loggin... -
Python樹林
Introduction to the Python logging Module: Why You Should Use Logging Instead of print() and Basic Settings
During development debugging or confirming operation in a production environment, it is common to use the print() function to check "how far the program has progressed" or "what the variable values are." However, in full-scale applicatio... -
Python樹林
Python Script Execution Control: Direct Run vs. Import
When writing Python scripts, you often see the following code at the end of the file: Python if __name__ == "__main__": main() This is often written as a standard "boilerplate," but it is actually an important control structure to determ... -
Python樹林
Introduction to Creating Python Packages: Directory Structure and Import Control with __init__.py
As the scale of a program grows and the number of module files (.py) increases, you will likely want to organize them into a single directory (folder). In Python, a collection of multiple modules organized by directory is called a "Packa... -
Python樹林
Creating and Importing Custom Python Modules: Splitting Code into Multiple Files
As a Python program grows in size, managing all the code in a single file (.py) becomes difficult. In such cases, it is common practice to split functions and classes into separate files based on their functionality and load (import) the...