8.1 Library os
Python has built-in libraries os and platform. They provide a bunch of functions for working with the operating system and getting info about it.
These libraries let you perform operations related to the file system, processes, environment variables, and other aspects of the operating system. Let's check out some of their functions now.
Library os
The os library provides functions for interacting with the operating system. It lets you perform operations related to the file system, process management, environment variables, and much more.
Getting the current working directory:
import os
cwd = os.getcwd()
print("Current Working Directory:", cwd)
Changing the current working directory:
os.chdir('/path/to/directory')
Creating a new directory:
os.mkdir('new_directory')
Deleting a directory:
os.rmdir('new_directory')
List files and directories:
files = os.listdir('.')
print("Files and directories:", files)
Heads up: The terms directory, catalog, and folder are synonymous. They just come from different operating systems. It’s all the same thing.
Also, it’s useful for you to learn how to work with environment variables. These are kinda like system variables that your program can read or set.
Getting the value of an environment variable:
home_dir = os.getenv('HOME')
print("Home Directory:", home_dir)
Setting the value of an environment variable:
os.environ['MY_VAR'] = 'my_value'
8.2 Library platform
The platform library provides functions to get info about the platform Python is running on. You can find out the operating system, processor architecture, Python version, and other info.
Main functions of the platform library
Operating System:
import platform
os_name = platform.system()
print("Operating System:", os_name)
Computer's network name (hostname):
node_name = platform.node()
print("Node Name:", node_name)
OS Release:
os_release = platform.release()
print("OS Release:", os_release)
OS Version:
os_version = platform.version()
print("OS Version:", os_version)
Processor Architecture:
architecture = platform.architecture()
print("Architecture:", architecture)
Processor Type:
processor = platform.processor()
print("Processor:", processor)
Python Version:
python_version = platform.python_version()
print("Python Version:", python_version)
Python Compilation:
python_compiler = platform.python_compiler()
print("Python Compiler:", python_compiler)
If you’re still curious, take a look at your computer through the eyes of your program — run this code:
import platform
print("Operating System:", platform.system())
print("Node Name:", platform.node())
print("OS Release:", platform.release())
print("OS Version:", platform.version())
print("Machine:", platform.machine())
print("Processor:", platform.processor())
print("Architecture:", platform.architecture())
print("Python Version:", platform.python_version())
print("Python Compiler:", platform.python_compiler())
8.3 Library sys
The sys library is a part of the Python standard library and provides access to variables and functions that interact with the Python interpreter. It’s useful for getting info about the runtime environment, managing the script execution process, and interacting with various system components.
Your program can do a lot of cool stuff with it.
1. Command Line Arguments
The sys library lets you grab command-line arguments passed to the script via the sys.argv list.
2. Exiting the Program
The sys.exit() function lets you end the program with a given exit status. Zero usually indicates successful termination, while any non-zero value indicates an error.
import sys
if len(sys.argv) < 2:
print("Error: not enough arguments")
sys.exit(1)
print("All arguments are specified correctly")
sys.exit(0)
3. Module Search Path
The sys.path list contains paths that the Python interpreter searches for modules to import. You can add new paths to this list to configure module search.
import sys
print("Module search paths:")
for path in sys.path:
print(path)
# Adding a new path
sys.path.append('/path/to/my/modules')
print("Updated search path list:", sys.path)
4. System Information
The sys library provides info about the system and Python interpreter, such as Python version and platform.
import sys
# Python Version
print("Python Version:", sys.version)
# Platform Information
print("Platform:", sys.platform)
# Size of an int in bytes
print("Size of int:", sys.getsizeof(0), "bytes")
5. Installed Modules
sys.modules is a dictionary containing info about all loaded modules. You can use it to get info about loaded modules or to re-import them.
import sys
# List of loaded modules
print("Loaded modules:")
for module in sys.modules:
print(module)
GO TO FULL VERSION