User Tools

Site Tools


other:python:misc_by_jyp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revision Both sides next revision
other:python:misc_by_jyp [2021/06/30 13:17]
jypeter created
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 errorThis 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>​+ 
 +==== 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 
 +>>>​ os.access('/​home/​jypmce/​.bashrc',​ os.W_OK) 
 +True</​code>​
  
 /* /*
- * tip template\\ <​code>​Some code</​code>​+==== Tip template ​==== 
 + 
 +<​code>​Some code</​code>​
  */  */
  
other/python/misc_by_jyp.txt · Last modified: 2024/04/19 12:02 by jypeter