Binary files with pickle#
Pickling (serialisation) converts a Python object into a byte stream; unpickling converts it back.
pickle.dump(obj, file)- write.pickle.load(file)- read one object; raisesEOFErrorat end of file.
पिकलिंग में पायथन ऑब्जेक्ट को बाइट्स में बदलकर बाइनरी फ़ाइल में लिखा जाता है (dump) और पढ़ते समय वापस ऑब्जेक्ट में बदला जाता है (load)। फ़ाइल के अंत में load() EOFError देता है, इसलिए try-except का प्रयोग करते हैं।
Updating a binary file#
Read all records into a list, change the required one, then write everything back with mode wb (or use seek() with rb+).
CSV files#
Use newline="" while opening CSV files so that extra blank lines are not added on Windows. Every value read from a CSV is a string.
CSV फ़ाइल में मान कॉमा से अलग होते हैं और इसे Excel में भी खोला जा सकता है। csv.writer से लिखते हैं (writerow/writerows) और csv.reader से पढ़ते हैं; पढ़े गए सभी मान स्ट्रिंग होते हैं।
Exam tips#
- The 5-mark question often asks: write
addRecord()andsearch()functions for a binary/CSV file.