Using With Block To Open Python Files

if we use with block to read a file line and than open file simply to read file lines 
So whether the file lines will open simply or not...??

# SOLUTION

# f = open("raaz.txt", "rt")
#
# f.close()
# Above open close syntax can be replaced by single statement
# open file using with block
# Everything else(means functions like readlines,seek,etc.) on f are same
# first create a text file "raaz" then run this program.
# with open("raaz.txt") as f:
# a = f.read(7)
# print(a)
# Output : Stud
# with open("raaz.txt") as f:
# a = f.readlines() # Give List of lines
# print(a)
# Output :
# ['Students should use LinkedIn to get connected with Corporate and Research world.\n',
# 'It will be very helpful for students.']

# Question of the day -
# Question 1:
# Can we do readlines outside with block?
# Yes or No and why?

# Answer :
# No, because with block closes the files once we get out of with block.

# Question 2:
# f = open("29. Tutorial.txt", "rt")
# Can we do readlines here?
# f.close()

# Answer :
# Yes

Comments

Popular posts from this blog

MAP FILTER AND REDUCE FUNCTION IN PYTHON

Writing Appending to a File

Recursions: Recursive Vs Iterative Approach