File Handling in Python | WD



File Handling in Python | WD | ninjasquad

In our previous Python tutorial, we have explained to make a Guess Game in Python. In this tutorial, we will explain about File Handling in Python.

Python allows file handling to perform file operation like create, read, write append content to files. It has built-in function read() and write() to perform operations with files.

So let’s code to play with files.

Opening a file in Python

We need to open file to perform any operation on file. We need to use built-in open() function to open file. The file can be opened for different purpose like read, write, append etc. So we need to specify the mode to open file.


file = open(filename, mode)

Supported file open mode:

  • r: Open file for a read operation.
  • w: Open an existing file for write operation. If file is not exist then file will be created.
  • a: Open an existing file for append operation.
  • r+: Open to read and write operation.
  • w+: Open to read and write operation.
  • a+: Open to read and append data.

Reading a file in Python

Now we will open file using open() function to read file data by passing r mode. The open() function returns a file object and loop through to print content. The below example will read a file in read mode and print line one by one.


file = open("webdamn.txt", "r")

for line in file:
    print (line)

We can also read the whole content of file using read() function.


file = open("webdamn.txt", "r")
print(file.read())

We can also read the parts of file using read() function and by specifying number of character that we want. The below example code will return first 3 characters of the file.


file = open("webdamn.txt", "r")
print(file.read(3))

We can also read the one line by using the readline() function. The below example code will return first line of file.


file = open("webdamn.txt", "r")
print(file.readline())

if we call readline() function two times then it will return first two lines from file.

Creating a file in Python

While oepning file with open() function and if pass file open mode w or a, then it will create a new file if file not exist.

The w – Write mode will create a file if the specified file does not exist.


file = open("webdamn.txt", "w")

The a – Append mode will create a file if the specified file does not exist.


file = open("webdamn.txt", "a")

Writing to a file in Python

For wrting to a file, we need to open file in write mode by passing w when open a file. Then we will write to file using write() function.


file = open('webdamn.txt','w')
file.write("writing content to file.")
file.write("wrting more content to file.")
file.close()

The above will overwrite the content into a specified file.

Append to a file in Python

We need to open the file in append mode by passing a when open a file to append data to a file. Then we will use write() function to append data to a specified file.


file = open('webdamn.txt', 'a')
file.write("Append some new data.")
file.close()

The above will append the content to the specified file.



Source: Internet

Leave a Comment

We are offering free coding tuts

X