Python Data Science Jobs & Interviews
20.4K subscribers
188 photos
4 videos
25 files
326 links
Your go-to hub for Python and Data Science—featuring questions, answers, quizzes, and interview tips to sharpen your skills and boost your career in the data-driven world.

Admin: @Hussein_Sheikho
Download Telegram
⁉️ Interview question
What happens when you open a file in Python using the mode `'r+b'` and immediately attempt to write to it without seeking to the end, assuming the file already exists and contains data?

😝 Answer:
When you open a file in `'r+b'` mode, you're opening it for both reading and writing in binary format. However, if you don't seek to the end of the file before writing, your writes will **overwrite existing data at the current file position**, which is typically the beginning unless you've moved the cursor. This can corrupt the original content, especially if the new data is larger than the portion being overwritten. The key insight is that **the file pointer starts at the beginning**, so even though the file was opened for reading, writing begins from the start unless explicitly moved. Additionally, this behavior may raise `OSError` or `IOError` if the file is locked or permissions are denied, but more commonly results in silent data corruption.

#️⃣ tags: #Python #AdvancedPython #FileHandling #BinaryFiles #FilePointer #DataCorruption #InterviewQuestion

By: t.iss.one/DataScienceQ 🚀