Python Module os: Difference between revisions
Jump to navigation
Jump to search
(7 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
* https://docs.python.org/3/library/os.html#module-os | * https://docs.python.org/3/library/os.html#module-os | ||
=Internal= | =Internal= | ||
* [[Python Language | * [[Python Language#os|Python Language | Standard Library]] | ||
* [[Python_Module_shutil|shutil]] | |||
=Overview= | =Overview= | ||
=Environment Variables= | =Environment Variables= | ||
To get an environment variable: | |||
<syntaxhighlight lang='python'> | <syntaxhighlight lang='python'> | ||
Line 13: | Line 16: | ||
print(os.environ.get('HOME')) | print(os.environ.get('HOME')) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
If the environment variable does not exist, the call will get <code>[[Python_Language#None|None]]</code> | |||
To set an environment variable: | |||
<syntaxhighlight lang='python'> | |||
import os | |||
os.environ['MY_VAR'] = 'myvalue' | |||
</syntaxhighlight> | |||
This works for testing. It does not change the value of the corresponding environment variable, if it exists, or it does not set a new one, if it doesn't, in the environment that calls the python interpreter. | |||
=Working Directory= | |||
{{Internal|File_Operations_in_Python#Working_Directory|File Operations in Python | Working Directory}} |
Latest revision as of 00:26, 22 July 2022
External
Internal
Overview
Environment Variables
To get an environment variable:
import os
print(os.environ.get('HOME'))
If the environment variable does not exist, the call will get None
To set an environment variable:
import os
os.environ['MY_VAR'] = 'myvalue'
This works for testing. It does not change the value of the corresponding environment variable, if it exists, or it does not set a new one, if it doesn't, in the environment that calls the python interpreter.