This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | Next revision Both sides next revision | ||
other:python:misc_by_jyp [2021/06/30 15:20] jypeter |
other:python:misc_by_jyp [2021/07/06 09:17] jypeter Cleaned |
||
---|---|---|---|
Line 5: | Line 5: | ||
</WRAP> | </WRAP> | ||
- | * Reading/setting environments variables\\ <code>>>> os.environ['TMPDIR'] | + | ==== Reading/setting environments variables ==== |
+ | |||
+ | |||
+ | <code>>>> os.environ['TMPDIR'] | ||
'/data/jypmce/climafcache' | '/data/jypmce/climafcache' | ||
>>> os.environ.get('SCRATCHDIR', '/data/jypmce/some_scratch_stuff') | >>> os.environ.get('SCRATCHDIR', '/data/jypmce/some_scratch_stuff') | ||
Line 14: | Line 17: | ||
</code> | </code> | ||
- | * Generating (aka //raising//) an error. This will stop the script, unless it is called in a function, and the code calling the function explicitely catches and deals with errors | + | ==== Generating (aka raising) an error ==== |
+ | |||
+ | This will stop the script, unless it is called in a function, and the code calling the function explicitely catches and deals with errors | ||
* <code>raise RuntimeError('\n\nOMG! An error! :-(\nAborting script...')</code> | * <code>raise RuntimeError('\n\nOMG! An error! :-(\nAborting script...')</code> | ||
* [[https://docs.python.org/3/tutorial/errors.html|Errors and Exceptions tutorial]] | * [[https://docs.python.org/3/tutorial/errors.html|Errors and Exceptions tutorial]] | ||
* [[https://docs.python.org/3/library/exceptions.html|Built-in Exceptions reference]] | * [[https://docs.python.org/3/library/exceptions.html|Built-in Exceptions reference]] | ||
- | * Stopping a script\\ <code>sys.exit('Some optional message about why we are stopping')</code> | ||
- | * Checking if a file/directory is writable by the current user\\ <code>>>> os.access('/', os.W_OK) | + | ==== Stopping a script ==== |
+ | |||
+ | A user can use ''CTRL-C'' or ''kill'' to stop a script, or ''CTRL-Z'' to suspend it temporarily (use ''fg'' to resume a suspended script). The code below can be used by the script itself to interrupt its execution, instead of raising an error | ||
+ | |||
+ | <code>sys.exit('Some optional message about why we are stopping')</code> | ||
+ | |||
+ | |||
+ | ==== Checking if a file/directory is writable by the current user ==== | ||
+ | |||
+ | <code>>>> os.access('/', os.W_OK) | ||
False | False | ||
>>> os.access('/home/jypmce/.bashrc', os.W_OK) | >>> os.access('/home/jypmce/.bashrc', os.W_OK) | ||
Line 27: | Line 40: | ||
/* | /* | ||
- | * tip template\\ <code>Some code</code> | + | ==== Tip template ==== |
+ | |||
+ | <code>Some code</code> | ||
*/ | */ | ||