open and read a file in python code example
Example 1: python read file
with open("file.txt", "r") as txt_file:
return txt_file.readlines()
Example 2: read file python
document = 'document.txt'
file = open(document, 'r')
file.seek(0)
for i in file:
k = i.strip()
print k
file.close()
with open(document) as ur:
for i in ur:
k = i.strip()
print k
Example 3: python open and read file with
with open('pagehead.section.htm','r') as f:
output = f.read()
Example 4: read and write to file python
file = open(“testfile.txt”, “r+”)