MCS-201 · Unit 12

Modules and Packages

Section 5 of 6

Block 3 · Unit 12Modules and Packages9 min · Section 5 of 6

12.5Standard Library Modules: Attributes, os and sys

Covers IGNOU MCS-201 §12.6

The standard library is the large collection of modules that comes with every Python installation: files, dates, maths, networking, JSON, databases and much more. This section looks at features shared by all modules and at two system modules, os and sys.

Attributes and tools for every module

ToolWhat it gives
help(module)Documentation of the module; help("modules") lists the installed modules
dir(module)A list of the names (functions, variables, classes) defined in the module
module.__name__The module’s name ("__main__" for the running script)
module.__file__The path of the file the module was loaded from (absent for modules built into the interpreter, such as sys)
module.__doc__The module’s docstring, or None

The os module

os lets a program work with the operating system: folders, paths, environment variables.

FunctionPurpose
os.getcwd()Current working directory
os.chdir(path)Change the current working directory
os.mkdir(path), os.makedirs(path)Create a folder (makedirs also creates missing parent folders)
os.rmdir(path)Remove an empty folder
os.listdir(path=".")Names of the entries in a folder
os.environDictionary-like access to environment variables
os.path.join(a, b)Join path parts with the right separator for the system

The sys module

sys gives access to the Python interpreter itself.

NamePurpose
sys.argvList of command-line arguments; sys.argv[0] is the script name
sys.pathModule search path
sys.version, sys.version_infoPython version as a string / as a tuple-like object
sys.platformOperating system identifier, e.g. "linux", "darwin", "win32"
sys.getsizeof(obj)Memory used by an object, in bytes
sys.maxsizeLargest index a container can have on this system
sys.exit(code)End the program (0 means success)

Key takeaways

  • The standard library is installed with Python; import modules before use.
  • help, dir, __name__, __file__, __doc__ work for every module.
  • os: folders, paths, environment; sys: arguments, path, version, exit.
  • sys.maxsize limits container sizes, not integer values.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What does dir(math) return?

Show answer

Answer: B. A list of the names defined in the math module

dir() lists names; help() shows documentation and os.getcwd() gives the current directory.

Question 2 · multiple choice

Which function returns the current working directory?

Show answer

Answer: C. os.getcwd()

getcwd means “get current working directory”; chdir changes it.

Question 3 · multiple choice

A script args.py contains import sys and print(len(sys.argv), sys.argv[1]). What does python args.py red blue print?

Show answer

Answer: C. 3 red

sys.argv is ['args.py', 'red', 'blue']: three items, and index 1 is “red”.

Question 4 · multiple choice

Which module attribute holds a module’s docstring?

Show answer

Answer: D. __doc__

__doc__ contains the documentation string written at the top of the module.

Question 5 · multiple choice

What does this program print?

Python · quiz_maxsize.py
import sys
big = sys.maxsize + 1
print(big > sys.maxsize, type(big).__name__)
Show answer

Answer: A. True int

Python integers grow as needed, so adding 1 to sys.maxsize simply gives a larger int.

Question 6 · short answer

Write a script that prints the names of all .py files in the current folder in alphabetical order.

Show worked answer

import os, then for name in sorted(os.listdir(".")): if name.endswith(".py"): print(name). os.listdir(".") returns the entries of the current folder in no guaranteed order, sorted() puts them in alphabetical order, and endswith(".py") keeps only Python files.

↑ ↓ move↵ openTab switch type