Leaves fall down, the wind sways those years old
This article is used as a record, some of the tricks encountered in the process of using python, some of the code is written by myself, some are Python built-in functions, and some are taken from the Internet. In this record, it is easy to find only for backup and forgetting.
This article will continue to be updated, and the content is limited to recording some code or modules that are commonly used but never remembered.
The console does not flash back
Get console size
Solve the problem of input prompt Chinese garbled
Accept multiple lines of input
Standard input and output
The function of print is similar to sys.stdout.write, because the default for print in 2.x is to assign output to standard output (sys.stdout).
Console color control (for windows)
Output color control (full platform)
More advanced usage can use the progressbar module.
Get the python installation path
Get the current python version
Get current time
View system environment variables
Get system disk
Get the current path (including the current py file name)
The line terminator used by the current platform
Get the terminal size
- return: returns the value of the function and exits the function.
- exit(): Exit directly.
- sys.exit(): Raises a SystemExit exception. If there is no catch error, the python program exits directly; after catching the exception, you can do some extra cleanup.
- sys.exit(0): Normal exit, others (1-127) are abnormal, and can throw exceptions for capture. (usually used to exit the program in the main thread)
- os._exit(0): Exit the python program directly, and the subsequent code will not be executed. (usually used to exit the program in the thread)
Domain name resolution to ip
Get server version information
Open function, use wb, rb instead of w, r
This way of writing is compatible with python2/3.
Output all file names in a directory
Find the path to the folder with the specified name
Judge data type
Format method splicing strings and variables
Remove the number after the decimal point
The first letter of the string is capitalized
Returns a string centered and padded with a space to a new string of length width.
List all letters
The judgment list is empty
Add single list elements
Add multiple list elements separately
Convert nested lists to a single list
Adding elements within the list
Generate a list of strings for a-z
If+else with list analysis
Multi-level nested list
Generating a generator and calling the next method can reduce memory overhead.
Replace key and value locations
Filter out duplicate keys
Dictionary sorting (py2)
Dictionary value judgment
When importing a module, set only the properties or methods that are allowed to be imported.
Import the package under the parent directory
Import modules under external directory
Add module properties
Then when we import a module, we can output dir(a) to see
Dynamically load all modules in a directory
Description: The exec function has no return value.
Description: The eval function has a return value
Output current time decorator
The result of this function execution is passed to the next time.
Enter list elements and serial numbers
Use signal to set the timeout for a function to execute
Using the retrying module to implement the function error retry function
If we run the have_a_try function, it will not end until random.randint returns 5, otherwise it will be re-executed. Please search for more usage of this module.
Use signal to implement ctrl+c to exit the program.
Use os.execl method to realize program self-restart
@2017.04.19Create this article
@2017.04.24Add eval/exec function
@2017.05.27 increase program operation, function timeout, function error retry
@2017.08.24Add format splicing string with variables, string derivation