other:python:misc_by_jyp
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
other:python:misc_by_jyp [2021/07/06 13:25] – Started the command-line args section jypeter | other:python:misc_by_jyp [2021/08/18 16:04] – Added pprint section jypeter | ||
---|---|---|---|
Line 2: | Line 2: | ||
<WRAP center round tip 60%> | <WRAP center round tip 60%> | ||
- | You will find on this page some useful, but unsorted, python tips and tricks that can't fit in a section of the main [[other: | + | You will find on this page some **useful, but unsorted, python tips and tricks** that can't fit in a section of the main [[other: |
</ | </ | ||
Line 39: | Line 39: | ||
True</ | True</ | ||
+ | ==== Working with paths and filenames ==== | ||
+ | |||
+ | If you are in a hurry, you can just use string functions to work with path and file names. But you will need some specific functions to check if a file exists, and similar operations. All these are available in 2 libraries that have similar functions. Both of these libraries can deal with Unix-type paths on Linux computers, and Windows-type paths on Windows computers | ||
+ | |||
+ | * [[https:// | ||
+ | * Available since... a long time! Use this if you want to avoid backward compatibility problems | ||
+ | * Some functions are directly in [[https:// | ||
+ | * [[https:// | ||
+ | * Available since Python version 3.4 | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | |||
+ | === Example: getting the size(s) of all the files in a directory === | ||
+ | |||
+ | < | ||
+ | $ ls -l | ||
+ | total 72 | ||
+ | -rw-r--r-- 1 jypmce ipsl 18147 Jun 25 2012 get_TS_cmip5.py | ||
+ | -rw-r--r-- 1 jypmce ipsl 16152 Jun 21 2012 get_TS_cmip5.py~ | ||
+ | -rw-r--r-- 1 jypmce ipsl 13954 Jul 3 2012 get_TS_cmip5_regular.py | ||
+ | -rw-r--r-- 1 jypmce ipsl 16539 Jun 22 2012 get_TS_cmip5_regular.py~</ | ||
+ | |||
+ | < | ||
+ | >>> | ||
+ | / | ||
+ | >>> | ||
+ | >>> | ||
+ | [' | ||
+ | >>> | ||
+ | >>> | ||
+ | [16152, 13954, 16539, 18147] | ||
+ | >>> | ||
+ | 64792</ | ||
==== Using command-line arguments ==== | ==== Using command-line arguments ==== | ||
- | === The fast but non-flexible way === | + | === The extremely easy but non-flexible way: sys.argv |
The name of a script, the number of arguments (including the name of the script), and the arguments (as strings) can be accessed through the '' | The name of a script, the number of arguments (including the name of the script), and the arguments (as strings) can be accessed through the '' | ||
Line 62: | Line 95: | ||
1 tas | 1 tas | ||
2 tas_tes.nc</ | 2 tas_tes.nc</ | ||
+ | |||
+ | === The C-style way: getopt === | ||
+ | |||
+ | Use [[https:// | ||
+ | |||
+ | === The deprecated Python way: optparse === | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | === The current Python way: argparse === | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | ==== Using ordered dictionaries ==== | ||
+ | |||
+ | Check the [[https:// | ||
+ | |||
+ | ==== Printing a readable version of long lists or dictionaries ==== | ||
+ | |||
+ | The [[https:// | ||
+ | |||
+ | < | ||
+ | |||
+ | >>> | ||
+ | |||
+ | >>> | ||
+ | OrderedDict([(' | ||
+ | |||
+ | >>> | ||
+ | OrderedDict([(' | ||
+ | | ||
+ | | ||
+ | {' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | </ | ||
/* | /* |
other/python/misc_by_jyp.txt · Last modified: 2024/11/04 15:01 by jypeter