Python樹林– category –
-
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... -
Python樹林
“Updating Failed. The response is not a valid JSON response” in WordPress: Cause and Solution (Dangerous Keyword in Title Case)
Summary: Sudden "The response is not a valid JSON response" Error When trying to save a post in the WordPress block editor, the following error suddenly appeared: Updating failed. The response is not a valid JSON response. I hadn't chang... -
Python樹林
How to Execute Strings as Expressions in Python: Basics of Dynamic Execution and Security Risks
Python has a built-in function that allows you to interpret and execute code (expressions) represented as strings as actual programs. This feature is very powerful and can be used for calculations based on user input or dynamic condition... -
Python樹林
How to Get Environment Variables in Python: Distinction between os.environ and os.getenv
When creating web applications or scripts, directly writing sensitive information like database passwords or API keys in the source code is a major security risk. It is recommended to save such information as "Environment Variables" mana... -
Python樹林
Pausing Python Execution: How to Use the time.sleep() Function and Specify Durations
When executing scripts, you often need to adjust the interval of API requests or add wait times between periodic tasks. In Python, you can pause the execution of a program for a specified duration using the sleep() function included in t... -
Python樹林
Python input() Function: How to Accept User Input from the Keyboard
When creating command-line tools or interactive scripts, there are times when you want to receive input from the user while the program is running. In Python, you can easily get standard input from the keyboard using the built-in input()... -
Python樹林
Terminating Python Scripts with sys.exit(): Controlling Exit Codes
When you run a Python script from the command line, a shell script, or another program, you need to tell the caller whether the process finished successfully or ended with an error. This is done using the "Exit Status (Exit Code)." Norma... -
Python樹林
Python’s assert Statement (Assertion): Condition Checks for Efficient Debugging and Caveats
When programming, there are "preconditions that must absolutely be true if the program is operating correctly," such as "the variable at this point must be a positive value" or "the list should not be empty." The mechanism to write these...