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 five characters of the file.

We also have other methods for reading like readline(). As the name suggests it reads the file line by line. If we pass an argument n inside the readline() function, the first n set of lines of the file will be printed as output.

Example,

f=open("file1.txt","r")
for i in f.readline():
    print(i)

The above code prints the entire content of the file line by line.

We also have a function called close() which is used for closing the opened file. It is a very good practice to always close the file when you are done with it.

For example,

f=open("file1.txt","r")
for i in f.readline():
    print(i)
f.close()


For writing the file we have write() function. This function overwrites the existing file or if the file does not exist it creates a file and writes on it.

f=open("file1.txt","w")
f.write("The new content")
f.close()

The above code overwrites the entire file with the given text.

We have append() function which is used for appending the file.
For example,

f=open("file1.txt","a")
f.append("The new content")
f.close()

Here the given line will be appended to the file.

We have x mode to create a new file, it returns an error if the file already exists.
For example,

f=open("file2.txt","x")

When the above code is executed, a new file named "file2" will be created. If the file name exists already it throws an error.

Exceptions in python :

1. ZeroDivisionError - The error is thrown if an integer is divided by zero.
2. IndexError - The error is thrown when we try to access an element that is out of index.
3. KeyError - The error is thrown when the key is not found.
4. FileNotFoundError - The error is thrown when the file is not found.
5. SyntaxError - The error is thrown in case of any mistake in the syntax.
6. IndentationError - The error is thrown when the indentation is wrong.
7. ValueError - The error is thrown when the builtin function for datatype has valid arguments but the arguments have invalid values specified.


Comments

Popular posts from this blog

Finding the percentage in python - HackerRank solution

HackerRank challenges with solutions (Rearrangement, Formula, Word sorting, Numbers, Sentence)

What's your name, Tuple - HackerRank solution in python