This module focuses on file handling in Python, covering different file operations, modes, file positioning, and string formatting techniques.
- Solution
- Description:
Task "Product Inventory":
- Implement two classes,
Product
andShop
. TheProduct
class represents a product with attributes such as name, weight, and category. TheShop
class manages the inventory by reading from and writing to a file, preventing duplicate entries.
- Implement two classes,
- Solution
- Description:
Task "Write and Remember":
- Implement a function
custom_write
that writes a list of strings to a file and returns a dictionary mapping the line number and byte position of each string.
- Implement a function
- Solution
- Description:
Task "Find It Everywhere":
- Implement a
WordsFinder
class that takes a list of file names and provides methods to get all words from the files, find the first occurrence of a word, and count the number of occurrences of a word across all files.
- Implement a
- Solution
- Description:
Task "String Formatting":
- Write code that formats strings using different formatting methods:
%
,.format()
, and f-strings, for various scenarios involving team statistics, competition results, and task completion times.
- Write code that formats strings using different formatting methods:
- Solution
- Description:
Task "File Operations in the OS":
- Use
os.walk
to traverse directories,os.path.join
to construct file paths, and methods such asos.path.getmtime
,os.path.getsize
, andos.path.dirname
to retrieve file modification times, sizes, and parent directories.
- Use