📋 Sys Module

Last Updated: 22th August 2025


The sys module gives access to system-specific parameters and functions. It’s very useful for CLI (command-line) programs.argparse is also a alternatives of sys module with more features.

List of Sys Functions:

  • sys.argv: Returns a list of command-line arguments. sys.argv[0] → script name and rest sys.argv[1:] → arguments.
# file: demo.py
import sys

print("Script name:", sys.argv[0])
print("Arguments passed:", sys.argv[1:])
# in cli -> python demo.py arg1 arg2 arg3
  • sys.exit(status): Exits the program with the specified status code.0 for success, 1 for failure.
import sys

print("Start program")
sys.exit(0)   # normal exit
print("This will not print")
  • sys.platform: Returns the platform name, e.g., "linux", "darwin", or "win32".
import sys
print(sys.platform)
  • sys.version: Returns the Python version as a string.
import sys
print(sys.version)
  • sys.path: Returns a list of directories in the Python path.
import sys
print(sys.path)
  • sys.stdout.flush(): Flushes the standard output buffer.
import sys, time

print("Hello...", end="", flush=False)
time.sleep(2)
sys.stdout.flush()
print(" World!")
#Without flush → "World!" may delay.
#With flush → Output prints immediately.
  • sys.getsizeof(object): Returns the size of the object in bytes.
import sys
a = [1,2,3,4,5]
print(sys.getsizeof(a))  # size in bytes