💻 The OS and SYS modules¶
1. The os Module¶
The os
module in Python provides a way of using operating system dependent functionality such as reading or writing to the environment, manipulating paths, handling files and more.
1.1 os.name¶
This function gives the name of the operating system dependent module imported.
import os
print(os.name) # outputs: 'posix' on Linux and 'nt' on Windows
posix
1.2 os.getcwd¶
This function allows you to see what your current working directory is.
import os
print(os.getcwd()) # e.g., outputs: '/home/user'
/workspaces/python_ml_course/docs/๐-Tutorials/๐-The-Standard-Library.ipynb
1.3 os.chdir¶
This function allows you to change your current working directory.
import os
os.chdir('../')
print(os.getcwd()) # outputs: '/path/to/directory'
/workspaces/python_ml_course/docs/๐-Tutorials
os.path.exists('file.txt') # returns True if file.txt exists
False
1.4 os.environ¶
This provides a dictionary of all environment variables. It's a mapping object representing the string environment.
import os
# creates a new environment variable using jupyter %env magic command
%env HELLO="HELLO PYTHON"
# take all environment variables and print the HELLO variable
print(os.environ["HELLO"])
env: HELLO="HELLO PYTHON" "HELLO PYTHON"
1.5 os.path¶
p = os.path.join('path', 'to', 'file.txt') # outputs: 'path/to/file.txt'
print(p)
print(os.path.split(p)) # outputs: ('path/to', 'file.txt')
print(os.path.dirname(p)) # outputs: 'path/to'
print(os.path.basename(p)) # outputs: 'file.txt'
print(os.path.splitext(p)) # outputs: ('path/to/file', '.txt')
path/to/file.txt ('path/to', 'file.txt') path/to file.txt ('path/to/file', '.txt')
2. sys Module¶
The sys
module provides access to some variables used or maintained by the Python interpreter and to functions that interact strongly with the interpreter.
2.1 sys.argv¶
sys.argv
is a list in Python, which contains the command-line arguments passed to the script.
import sys
print(sys.argv[0]) # print out the name of the script
/usr/local/lib/python3.9/site-packages/ipykernel_launcher.py
Run this script from the command line with some arguments to see how it works: python script.py arg1 arg2
2.2 sys.exit¶
This function allows you to terminate the Python script. It takes an optional argument, which is an integer giving the exit status (zero is the default, and "normal termination").
import sys
# sys.exit() # comment out to run the rest of the code
2.3 sys.version¶
This can be used to get information about the version of Python currently being used.
import sys
print(sys.version) # e.g., outputs: '3.9.5 (default, May 11 2021, 08:20:37) [GCC 10.2.0]'
3.9.17 (main, Jun 6 2023, 23:29:09) [GCC 10.2.1 20210110]
2.4 sys.path¶
This is an environment variable that is a search path for all Python modules.
import sys
print(sys.path)
['/workspaces/python_ml_course/docs/๐-Tutorials/๐-The-Standard-Library.ipynb', '/usr/local/lib/python39.zip', '/usr/local/lib/python3.9', '/usr/local/lib/python3.9/lib-dynload', '', '/home/vscode/.local/lib/python3.9/site-packages', '/workspaces/python_ml_course', '/usr/local/lib/python3.9/site-packages']
This will output a list of paths where Python looks for modules when a script imports them.