How To Read A File In Python

How To Read A File In Python

In this example i will show you how to read a file in python step by step. We can read a file in python in different ways. to read a file in python we use python inbuilt open() function.

How To Read A File In Python

procedure 1:

x=open("devops.txt","r")
y=x.read()
print(y)

Syntax:

open(filename, mode)

the mode attribute of a file object tells you which mode a file was opened in.

in above example we used read mode(r).

r-Read mode which is used when the file is only being read.

name attribute tells you the name of the file that the file object has opened

to read the file we use read()method in python

procedure 2:

with open("devops.txt","r") as x:
  y=x.read()
  print(y)

it will also give you the same output. you can use any one of the above method to read a file in python.

  • how to read a file in python
  • python how to read a file
  • python read file

Leave a Reply

Your email address will not be published. Required fields are marked *