Let’s look at files now. How to access and write in files using Python.  First will look at text files only, i.e, how to load/open txt file and append or write data.

So, there is open method.

It is used to read files. It takes arguments (filename, mode). Mode can be -:

  1. r -> It is the read mode. In this you can only read the file can’t manipulate it.
  2. w -> It is the write mode. In this you can write to file and the current information will be erased.
  3. a -> It opens the file for appending any data written to the file is automatically added to the end.
  4. r+ -> It is both read and write.

Mode argument is optional, by default -: ‘r’

There are two ways of accessing and writing to files. We will walk through examples -:

## with simply open() method, we are trying to open the file and modify it.
## Created one file already in my cwd. If you want to fetch from other directory, just ## pass the path 

file = open('file1.txt','r')

In [2]:

## It will print the file object.


<_io.TextIOWrapper name='file1.txt' mode='r' encoding='cp1252'>

In [3]:

## read()-> It will print all the lines/contents with new line character.



'1. "My name is XYZ".\n2. "Currently I am persuing Bachelor\'s of Technology".\n3. "I have done several projects on Software  Development".'

In [4]:

## Here we have to close the file otherwise in large program it might create a proble. ## That's the reason, to open file with "with" keyword. Let's see how.

In [5]:

## Here opening the file with "with" keyword and it will automatically close the file after before coming out of the with.

with open('file1.txt','r') as f:

1. "My name is XYZ".
2. "Currently I am persuing Bachelor's of Technology".
3. "I have done several projects on Software  Development".

In [6]:

## Checking if file is closed.


Checking different methods of file object

In [7]: tell() , seek()

with open('file1.txt','r') as f:

 ## It will print each line and make the pointer to point at next line.              print(f.readline())

        ## It is telling the current position of the pointer

        ## Using seek we can modify the current pointer by passing the position as paramter in seek.

        ## Checking if pointer comes to 0 or not.

        ## Printing out the line again to check what will it prints.           print(f.readline())

1. "My name is XYZ".
1. "My name is XYZ".


It will return list of the lines in the file.

In [8]:

with open('file1.txt','r') as f:

['1. "My name is XYZ".\n', '2. "Currently I am persuing Bachelor\'s of Technology".\n', '3. "I have done several projects on Software  Development".']

Writing in files

In [9]:

## If file is not created then we can also create it like that.
## It will automatically generate new file in the current directory.

with open('file2.txt','w') as f1:

If we will go to our file then it will be empty right now.

In [10]:

## Writing to the created file.

with open('file2.txt','w') as f1:

If we will go to the file2.txt then it will write Hello in that file.

In [11]:

## Again writing then it is remove the previous contents. So in order to avoid the removal of previous content.
## One can use the append mode, it will append the data in already existing file.

with open('file2.txt','w') as f1:

If we will go to the file2.txt now then it will write Test1 and remove the previous written content i.e, Hello.

In [12]: Appending in files

## using append('a') mode, it will write to existing file without removing previous content.

with open('file2.txt','a') as f1:

So, now we appended the data then it will update/ add new content instead of replacing with new one. So the content in our text file is as follows -:

Test1 Test2

So, this is all for files handling in Python. Hope you enjoyed learning it.