Posted by: Sourav | March 24, 2017

Basic File Read Operation in Python 2.7,Python Teacher Sourav,Kolkata 09748184075


textfile=open(“read_it.txt”,”r”)
textfile.close
print(“\nReading characters from file”)
textfile=open(“read_it.txt”,”r”)
print(“\n”+textfile.read(1))
print(“\n”+textfile.read(5))
textfile.close

print(“\nReading whole file”)
textfile=open(“read_it.txt”,”r”)
print(“\n”+textfile.read())

textfile.close

print(“\nReading one line at a time”)
textfile=open(“read_it.txt”,”r”)
print(“\n”+textfile.readline())
print(“\n”+textfile.readline())
print(“\n”+textfile.readline())
textfile.close

print(“\nReading entire file in a list”)
textfile=open(“read_it.txt”,”r”)
lineslist=textfile.readlines()
print(“\n”+”the length of the list is “+str(len(lineslist)))
for lines in lineslist:
print(“\n”+lines)
textfile.close

print(“\nlooping through the file line by line”)
textfile=open(“read_it.txt”,”r”)

for lines in textfile:
print(“\n”+lines)
textfile.close

raw_input(“\n\nPress the enter key to exit.”)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: