Commonly used standard library modules including os, sys, datetime, and json, each explained with a short example.
35 cards · basic cards · AI-written, checked twice. Edit anything.
- What does os.getcwd() do?
- Returns the current working directory as a string.
- What does os.listdir(path) return?
- A list of filenames and directory names in the given path.
- What does os.path.join('dir', 'file.txt') do?
- Concatenates path components using the OS-specific separator.
- What does os.makedirs('a/b/c') do?
- Creates nested directories, including parent directories if needed.
- What is sys.argv in Python?
- A list containing command-line arguments passed to the script.
- What does sys.exit(1) do?
- Terminates the program and returns the exit code 1 to the system.
- What does sys.version return?
- A string describing the Python version and build information.
- What does datetime.datetime.now() return?
- A datetime object representing the current date and time.
- What is datetime.timedelta used for?
- Represents a duration, the difference between two dates or times.
- What does strftime('%Y-%m-%d') do?
- Formats a datetime object as a string using the given format code.
- What does json.dumps(dict) do?
- Converts a Python dictionary to a JSON-formatted string.
- What does json.loads(string) do?
- Parses a JSON string and returns a Python dictionary or list.
- What does json.dump(data, file) do?
- Writes a Python object as JSON directly to a file.
- What does collections.Counter() do?
- Counts hashable objects and stores the counts in a dictionary-like object.
- What does collections.defaultdict(int) do?
- Creates a dictionary that returns a default value (int, 0) for missing keys instead of raising KeyError.