Python Practice › sys Module Programs
Python sys Module Programs with Solutions
20 sys module programs: command-line arguments, exit codes, stdin/stdout/stderr, the module search path, version checks, recursion limits and object sizes. Programs that need arguments run a small helper script so you can see real results.
Before you start
Read the sys Module and Modules & pip lesson first if the topic is new to you.
All programs
- #1Read command-line arguments
Write a Python program that prints its command-line arguments.Easy - #2Sum numbers from the command line
Write a Python program that adds the numbers passed as arguments.Easy - #3Exit with a status code
Write a Python program that exits with code 1 on failure and show the code the caller sees.Easy - #4Check the Python version
Write a Python program that checks it is running on Python 3.10 or newer.Easy - #5Detect the operating system
Write a Python program that prints a different message per operating system.Easy - #6Write errors to stderr
Write a Python program that writes normal output to stdout and errors to stderr.Easy - #7Read numbers from stdin
Write a Python program that reads numbers from standard input and prints their total.Easy - #8Write with sys.stdout
Write a Python program that prints a progress bar withsys.stdout.write.Easy - #9Inspect sys.path
Write a Python program that shows how many folders Python searches for modules.Easy - #10Import from a custom folder
Write a Python program that adds a folder tosys.pathand imports a module from it.Medium - #11Which modules are loaded?
Write a Python program that checks whether a module has been imported yet.Easy - #12Size of objects
Write a Python program that compares the memory size of a few objects.Easy - #13Recursion limit
Write a Python program that reads the recursion limit and handles RecursionError.Easy - #14Largest list index
Write a Python program that printssys.maxsizeand shows ints can be bigger.Easy - #15Float limits
Write a Python program that prints the largest float and float precision.Easy - #16Which Python is running?
Write a Python program that checks which interpreter file is running.Easy - #17Byte order and encoding
Write a Python program that prints the machine’s byte order and the default text encoding.Easy - #18Exit with a message
Write a Python program that stops with a message when a required file is missing.Easy - #19Arguments with argparse
Write a Python program that parses named options with argparse.Medium - #20Current exception info
Write a Python program that reads the active exception withsys.exception().Easy