Read() Function in Python | Python built-in functions | Python tutorials for beginners

Published: 05 May 2020
on channel: ViaDigitally
29
1

#Builtinfunction #ReadFunction #PythonTutorials

Read() Function in Python:
1. This is a built-in function, this function is used to handle the files.
2. This is used to read the data from the given file.
3. In python, the read() is avaiable 4 forms:
a). read()
b). read(n)
c). readline()
d). readlines()
a). read():
This furnction reads the entire data from the given file.
syntax:
read()
ex: to read entire data from a file:


fp = open('sample.txt','r') #to open r mode
d=fp.read() #read total data
print('The sample.txt file content is:')
print(d)

b). read(n):
This function is used to read the 'n' characters only
syntax:
read(n)
here, n is size .

ex: to read first 8 characters from a file:

fp = open('sample.txt','r') #to open r mode
d=fp.read(8) #read 3 characters only
print('The sample.txt file content is:')
print(d)

3. readline():
This function is used to read only one line
syntax:
readline()

ex: to read first line

fp=open('sample.txt','r')
a=fp.readline()
print(a)

ex-2: to read first line

fp=open('sample.txt','r')
a=fp.readline()
b=fp.readline()
c=fp.readline()
print(a)
print(b)
print(c)
fp.close()
d). readlines():
This function is used to read all lines from
a given file.
this funtions returns the result as list type.
ex: to read entire data from a file:
fp=open('sample.txt','r')
d=fp.readlines()
print(d)
for i in d:
print(i)
fp.close()
---------------------------------------------
Python tutorial for beginners| python programming language tutorial
Python basics | python tutorial for beginners | learn python programming from scratch
Python tutorial for beginners - learn python for machine learning and web development.
Python tutorial for beginners | python programming | learn python | great learning.
Python tutorial for beginners [full course] learn python for web development.


On this page of the site you can watch the video online Read() Function in Python | Python built-in functions | Python tutorials for beginners with a duration of hours minute second in good quality, which was uploaded by the user ViaDigitally 05 May 2020, share the link with friends and acquaintances, this video has already been watched 29 times on youtube and it was liked by 1 viewers. Enjoy your viewing!