Posts

Showing posts with the label Files and exceptions in python

Files and exceptions in python

  File Concepts in python : The main function in file execution is the  open()  function which is used for opening the required files. The files can be opened in four modes namely, "r" - read mode, here the file can only be read and no other permission is granted. It is the default mode. "w" - write mode, here the file is opened for writing alone "a" - append mode, here the file grants access for appending "x" - create mode, for creating a new file example, open("file1.txt","r") The above line of code opens the file named file1 in read mode. If we try to write on this file, it throws an error. We have  read()  method for reading the file. For example, f= open("file1.txt","r") print(f.read()) When the above set of lines is implemented, it prints all the contents available in the file as f.read() reads the entire file. If we pass argument inside the read function i.e.  read(5),  we'll be getting the first