Python Tutorials - Herong's Tutorial Examples - v2.14, by Herong Yang
What Is "os" Module
This section describes the 'os' module, which provides you an interface to the operating system where the Python script in running.
What Is "os" Module? "os" is a Python module that provides you an interface to the operating system where the Python script in running.
Some commonly used properties and methods of the "os" module are:
1. os.name, os.linesep, os.pathsep, os.sep - Properties hold information about the operating system.
>>> import os >>> os.name 'posix' # Separator between lines in a text file >>> os.linesep '\n' # Separator between paths in an environment variable >>> os.pathsep ':' # Separator between parts in a file/directory path >>> os.sep '/'
2. os.environ - Property holds a dictionary of environment variables of the operating system.
>>> for key in os.environ: ... print(key, os.environ[key]) ... TERM_PROGRAM Apple_Terminal TERM xterm-256color SHELL /bin/bash USER herong PATH /Library/Frameworks/Python.framework/Versions/3.8/bin:... LANG en_US.UTF-8 HOME /Users/herong ...
3. os.chdir(path) - Method changes the current working directory to the given path.
>>> os.chdir('/Users') >>> os.getcwd() '/Users'
4. os.getcwd(path) - Method returns the current working directory. See above example.
5. os.getenv(name) - Method returns the value of the given system environment variable name.
>>> os.getenv('USER') 'herong' >>> os.getenv('User')
6. os.os.get_exec_path() - Method returns a list of paths of executable programs.
>>> os.get_exec_path() ['/Library/Frameworks/Python.framework/Versions/3.8/bin', '/usr/local/bin', '/usr/bin', '/bin', '/usr/sbin', '/sbin', '/opt/X11/bin' ]
7. os.getpid() - Method returns the process id of this running Python script.
>>> os.getpid() 95866
8. os.putenv(name, value) - Method sets a system environment variable to be used for new child processes. To a system environment variable for the current process, you can add it to the os.environ property.
>>> os.putenv('PI', '3.14') >>> os.getenv('PI') >>> os.environ['PII'] = "3.14158" >>> os.getenv('PII') '3.14158'
9. os.times() - Method returns the current global process times as a posix.times_result object.
>>> os.times() posix.times_result(user=0.06, system=0.05, children_user=0.03, children_system=0.03, elapsed=1656793820.55) >>> os.times().system 0.05 >>> os.times().elapsed 1656793901.12
10. os.urandom(size) - Method returns a random byte sequence of "size" bytes.
>>> os.urandom(8) b'0\xf0\xc5\xed\nhRL' >>> os.urandom(8) b'\x84\x18\xcd\x85G\xdan\xbd' >>> os.urandom(8) b'\xbb\xf70\xb6.\xef\xbbz'
"os" module also offers a number of methods to create child processes and directories/files. See next tutorials for more details.
Table of Contents
Variables, Operations and Expressions
Function Statement and Function Call
Iterators, Generators and List Comprehensions
Packages and Package Directories
Use "os" Module to Create Child Processes
Use "os" Module to Manage Files
"pathlib" - Object-Oriented Filesystem Paths
"pip" - Package Installer for Python
SciPy.org - Python Libraries for Science
pandas - Data Analysis and Manipulation
Anaconda - Python Environment Manager