Academy for Academic Education

Python Classroom Series – 29/Sept/2020

More about files

  • Common file Operations:
    • file_write = open(r’C:\temp\test.txt’, ‘w’) => Create a file for writing
    • file_object = open(‘data.txt’,’r’) => Create a file for reading
    • file_object.read() => read entire file into a string
    • file_object.read(N) => Read upto next N Character (or bytes) into a string
    • file_object.readline() => Read the nextline
    • file_object.readlines() => Read all the lines into a list of strings
    • file_write.write(<string>)
    • file_write.writelines(<list>)
    • file_write.close()
  • Write a binary file with write()
  • Files in Text Mode
    • Dealing with strings
  • Files in Binary Mode
    • Dealing with bytes
  • Store Python Objects into files
    • Writing Python Objects by formatting a text file is a possible option Refer Here
    • Storing Native Python Objects: pickle
      • The pickle module performs what is known as object serialization
      • The process of writing the objects to file (serialization) is referred as pickle or pickling and the process of reading objects into memory from file (deserialization) is referred as unpickling
    • Applying Pickle to samples and also to lt_restaurant is over here

Next Steps:

  1. Understanding JSON
  2. Understanding YAML
  3. Using JSON to store & retrieve the data in python
  4. Using YAML to store & retrieve the data in python
Exit mobile version