- OS module: This module provides a portable way of interacting with the operating system. It allows you to perform various operating system-related tasks such as file and directory manipulation, process management, environment variables, etc.
- SYS module: This module provides access to some variables used or maintained by the Python interpreter and to functions that interact strongly with the interpreter. It provides information about constants, functions, and methods of the Python interpreter. It’s often used for system-specific parameters and functions.
- Here’s a basic example showcasing the differences between the two:
- import OS
import sys# Example using OS module
# Get current working directory
print(“Current working directory:”, OS.getcwd())# List files and directories in the current directory
print(“Files and directories in current directory:”)
print(OS listdir())# Example using sys module
# Get the version of Python
print(“Python version:”, sys.version)# Get the command line arguments passed to the script
print(“Command line arguments:”, sys.argv) -
In this example, the OS module is used to get the current working directory and list the files and directories in it, while the
sys
module is used to print the Python version and the command-line arguments passed to the script.OS Module:
The
os
module provides a way to interact with the operating system in a platform-independent manner. It abstracts many operating system-specific functionalities into a simple Python interface.- File and Directory Operations:
os.getcwd()
: Returns the current working directory as a string.os.listdir(path='.')
: Returns a list of the names of the files and directories in the specified directory.os.chdir(path)
: Changes the current working directory to the specified path.
- File and Directory Manipulation:
os.rename(src, dst)
: Renames a file or directory fromsrc
todst
.os.remove(path)
: Removes (deletes) the file specified bypath
.os.mkdir(path)
: Creates a directory with the specifiedpath
.os.rmdir(path)
: Removes (deletes) the directory specified bypath
.
- Process Management:
os.system(command)
: Executes the command specified bycommand
in a subshell.os.spawnv(mode, path, args)
: Spawns a new process using the executable specified bypath
with argumentsargs
.os.kill(pid, sig)
: Sends the signalsig
to the process with the process IDpid
.
- Environment Variables:
os.environ
: A mapping object representing the environment variables available to the process.
SYS Module:
The
sys
module provides access to some variables used or maintained by the Python interpreter, and functions that interact strongly with the interpreter.- System-specific Parameters:
sys.argv
: A list of command-line arguments passed to a Python script.sys.version
: A string containing the Python interpreter version.
- System-specific Functions:
sys.exit([arg])
: Exits from Python. Optionally, the exit statusarg
can be given.sys.platform
: A string identifying the platform where Python is running (e.g., “win32”, “linux”, “darwin”).sys.getsizeof(object[, default])
: Returns the size of an object in bytes.sys.getfilesystemencoding()
: Returns the name of the encoding used to convert Unicode filenames to system file encoding.
- Interpreter-related Functions:
sys.path
: A list of strings that specifies the search path for modules.sys.modules
: A dictionary mapping module names to modules which have already been loaded.
By leveraging the functionalities provided by these modules, Python programs can interact with the operating system and the Python interpreter effectively and efficiently, making them versatile and adaptable across different environments.
- File and Directory Operations:
OS VS SYS MODULE IN PYTHON
The OS and SYS modules in Python are both useful for interacting with the operating system, but they serve different purposes.