mori– Author –
-
Python樹林
Converting Numbers to Binary, Octal, and Hexadecimal Strings in Python: Using bin, oct, and hex
In programming, you often need to convert decimal numbers into formats that computers interpret easily, such as binary, or formats used in memory dumps, such as hexadecimal. Python provides convenient built-in functions to convert number... -
Python樹林
Handling Binary, Octal, and Hexadecimal Numbers in Python: Numerical Prefixes and Notation
When handling numbers in Python programs, we usually use decimal numbers (0-9). However, depending on the application, it may be more intuitive to use binary, octal, or hexadecimal. For example, binary is suitable for bitwise operations,... -
Python樹林
Creating Directory Hierarchies in Python: Using os.makedirs and the exist_ok Option
When outputting files, ensuring the destination directory (folder) exists beforehand is necessary. The Python standard library os module provides two functions for creating directories: os.mkdir() and os.makedirs(). os.makedirs() is part... -
Python樹林
How to Delete Files and Directories in Python: Using os.remove and shutil.rmtree
In file manipulation programs, deleting unnecessary temporary files or directories containing old data is often necessary. In Python, the function used differs depending on whether the target is a "file" or a "directory (folder)". This a... -
Python樹林
Copying Files and Directories in Python: A Complete Guide to shutil.copy and shutil.copytree
Copying files or directories is essential when creating data backups or replicating template folders for new projects. The Python standard library shutil module (Shell Utilities) allows executing these operations with simple function cal... -
Python樹林
How to Move and Rename Files or Directories in Python: Using shutil.move
In scenarios like organizing files, rotating logs, or processing backups, you often need to move files or directories to a different location. To do this in Python, using the move() function from the standard library shutil module is the... -
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...