October 11

File Handling

Python has built-in function ‘open’ which open file for read/write operation and returns a file object.
Open method takes three parameter filename, mode of opening file, buffering. Only filename is mandatory others are optional.
Some Important mode of opening file:

Some important tips while working with file :

  • Always remember to read files line by line than reading them as a whole. Sometimes you may have to read files which are way bigger than your available RAM
  • try to use with statement. It will take care of closing the file for you
  • Always remember to close the file because there is an upper limit to the number of files a program can open