All fields

“File I/O & Context Managers” in Information Technology

27 questions across 1 technology.

Information Technology

27 questions in 1 technology

#QuestionTechnologyTopicLevel
1How would you implement asynchronous file I/O using aiofiles alongside an async context manager, to avoid blocking the event loop during file operations? Python › File I/O & Context ManagersPythonFile I/O & Context ManagersAdvanced
2How do you implement contextlib.ExitStack to dynamically manage a VARIABLE number of context managers determined at runtime? Python › File I/O & Context ManagersPythonFile I/O & Context ManagersAdvanced
3How do you use multiple context managers in a single 'with' statement, and how does this compare to nesting separate 'with' blocks? Python › File I/O & Context ManagersPythonFile I/O & Context ManagersIntermediate
4How would you write a context manager that atomically writes to a file, avoiding leaving a corrupted or partially-written file if an exception occurs mid-write? Python › File I/O & Context ManagersPythonFile I/O & Context ManagersAdvanced
5How do you open and read a text file using the 'with' statement, and why is 'with' preferred over manual open()/close()? Python › File I/O & Context ManagersPythonFile I/O & Context ManagersBeginner
6What is the difference between reading a file with .read(), .readline(), and .readlines()? Python › File I/O & Context ManagersPythonFile I/O & Context ManagersBeginner
7What are the common file modes ('r', 'w', 'a', 'rb') passed to open(), and what does each one do? Python › File I/O & Context ManagersPythonFile I/O & Context ManagersBeginner
8How do you write and use a custom context manager by implementing __enter__ and __exit__? Python › File I/O & Context ManagersPythonFile I/O & Context ManagersIntermediate
9How does @contextlib.contextmanager let you write a context manager using a generator function instead of a full class? Python › File I/O & Context ManagersPythonFile I/O & Context ManagersIntermediate
10How do you use pathlib.Path for modern, object-oriented file path manipulation instead of os.path's string-based functions? Python › File I/O & Context ManagersPythonFile I/O & Context ManagersIntermediate
11How do you check whether a file or directory exists before operating on it, using pathlib? Python › File I/O & Context ManagersPythonFile I/O & Context ManagersIntermediate
12How would you implement a context manager that temporarily changes the current working directory and restores it afterward? Python › File I/O & Context ManagersPythonFile I/O & Context ManagersAdvanced
13How would you write a reusable context manager decorator using contextlib.contextmanager that ALSO works when applied directly to a function, like @contextmanager-decorated functions support? Python › File I/O & Context ManagersPythonFile I/O & Context ManagersAdvanced
14How would you implement a context manager for managing a lock (like threading.Lock) while ALSO adding a timeout, raising an error if the lock can't be acquired quickly enough? Python › File I/O & Context ManagersPythonFile I/O & Context ManagersAdvanced
15How do you correctly handle character encoding when reading text files that might not be UTF-8, and what error occurs if you guess wrong? Python › File I/O & Context ManagersPythonFile I/O & Context ManagersAdvanced
16How do context managers and the `del` statement relate to timely memory cleanup versus relying on garbage collection? Python › Memory Management & Garbage CollectionPythonMemory Management & Garbage CollectionIntermediate
17How does context manager __exit__ interact with exception handling, and how can it SUPPRESS an exception by returning True? Python › Exception HandlingPythonException HandlingAdvanced
18How would you write a context manager using @contextlib.contextmanager that both sets up a resource AND properly handles/logs any exception that occurs while it's in use? Python › Exception HandlingPythonException HandlingAdvanced
19How does the iterator protocol interact with the with-statement for resource-managing iterables like file objects? Python › Iterators & the Iterator ProtocolPythonIterators & the Iterator ProtocolAdvanced
20How do RotatingFileHandler and TimedRotatingFileHandler prevent log files from growing unbounded? Python › LoggingPythonLoggingAdvanced
21How do context manager magic methods __enter__ and __exit__ work, and what does __exit__'s return value control? Python › Magic Methods & Operator OverloadingPythonMagic Methods & Operator OverloadingAdvanced
22What does the 'finally' block guarantee, and when is it typically used? Python › Exception HandlingPythonException HandlingBeginner
23How do you manually exhaust and inspect an iterator without a for-loop? Python › Iterators & the Iterator ProtocolPythonIterators & the Iterator ProtocolBeginner
24How does Python's iter() function work with a sentinel value as a second argument? Python › Iterators & the Iterator ProtocolPythonIterators & the Iterator ProtocolIntermediate
25How would you write a descriptor supporting attribute DELETION via __delete__, and what real use case does that enable? Python › Descriptors & PropertiesPythonDescriptors & PropertiesAdvanced
26Why is logging generally considered thread-safe, and what should you still be careful about in multiprocessing? Python › LoggingPythonLoggingAdvanced
27Why is __del__ (the finalizer method) discouraged for critical cleanup logic, and what problems can it cause? Python › Memory Management & Garbage CollectionPythonMemory Management & Garbage CollectionAdvanced