Python Basic Tutorials

Python is a high level general purpose programming language which has a clear/easy learning curve. Python programming language is massively used in various domains like Artificial Intelligence, Data Science, Web Development, Utilities Tools and Scripts and many more

Python is a Programming language, and the default Python we use is written in C programming language which is also referred to as CPython (Python implementation in C). There are various implementation of Python Programming language i.e Jython(in JAVA), Skulpt(in JS) e.t.c

To make everything easy: We refer CPython as Python

File Operations in Python

Files are used to store the data permanently. These are basically used to store the information. Files stores data permanently in non-volatile memory like a hard disk. Different file operations in python are:

  • Opening a file.
  • Reading from a file.
  • Writing to the file.
  • Closing a file.
Opening a file.

we can open a file using open() function either in read or in write mode. The open() function takes two parameters, filename and opening mode. After calling this function, if a given filename exists then this function will return that existing file object.

Syntax open(filename, mode)
The different modes available are:
  • "r" for reading.
  • "w" for writing.
  • "a" for appending.
  • "r+" for both reading and writing.
  • "t" for opening a file in text mode.
  • "b" for opening a file in binary mode.

>>>f=open("Hello.txt") #opens file in current directory.
>>>f=open("Hello.txt","r") #opens a existing file in read mode.
>>>f=open("Hello.txt","w") #opens a file in write mode,
                           # if there's no such named file
                           # then it will automatically create a
                           # new file and open it.

Writing to a file.

To write something to a file, we need to open a file write mode(w) or in append mode(a). Python provides the write() method to write text or sequence of bytes (i.e, binary file) to the file. When we open a file in 'w' mode, it will create a new file with a given filename if that named file does not exist else it will open that file overwriting all the content that means contents inside the file get erased.


f.write("Hello, python!")
f.write("\nI love python.")

This will create a file called Hello.txt where the following text will be written to.

    Hello, python!
    I love python.

Let us open this Hello.txt file in an append(a) mode.

with open('test.txt','a') as file:
    file.write("\nPython is awesome")
    file.write("\nPython is fun.")

This will append the sentences to the end of the file as follows:

Hello, python!
I love python.
Python is awesome
Python is fun
Reading from a File.

To read the file in python, we should open our file in read mode(r). There is a method called read() which takes the size of the data to be read as a parameter.

If the parameter is not passed, it will read all the remaining content to the end of the file.

Let us read the content from Hello.txt that we have created.

>>> #reads first five characters from the file.

>>> #reads next ten characters from the file.
   ', python!\n'

>>> #reads next 30 characters from the file.
   'I love python.\nPython is awesom'

>>> #reads entire remaining file content.
   'e\nPython is fun.'

In python, there is another method, called readline() which is used to read the individual lines from the file. i.e,

   'Hello python!\n'
   'I love python.\n'

   'Python is awesome\n'

Also, readlines() method reads the entire remaining file content to the end of the file. i.e,

   [', Python is fun.']
Closing a file

After all the file operations are done, it is a good programming practice that we should always close a file. Closing a file in python is done using the close() method.



This is not the proper way to close a file. This way has no surety that the file will always be closed. If any exceptions occur, the program will be terminated without closing a file. So, a better way would be using exception handling.


Another way is to use the 'with' block. In this way, there's no need to close a file explicitly, it is done internally.

with open('Hello.txt','r') as file:
    # different file operations