In [ ]:
#difference read() and readlines()
#read reads and return entire content in one time ,while readlines read and retunr entire content in list form
In [ ]:
#difference readline() and readlines()
ans:readline read and return a single line ,while readlines read and retunr all content in line's form
In [4]:
#Q write a single loop to display all content of a text file 'poem.txt' after removing leading anf trailing whitespace
f=open('poem.txt','w')
f.write(' twinkle twinkle \n')
f.write(' little star \n')
f.write(' what r wonder \n')
f.close()
f=open('poem.txt','r')
lns=f.readlines()
for line in lns:
print(line.strip()) #strip function remove both leading and trailing space from line
In [15]:
#Q write a single loop to display all content of a text file 'poem.txt' after removing leading anf trailing whitespace
f=open('poem.txt','w')
f.write(' twinkle twinkle \n')
f.write(' little star \n')
f.write(' what r wonder \n')
f.close()
f=open('poem.txt','r')
lns=f.readlines()
for line in lns:
print(line.strip(),end='') #strip function remove both leading and trailing space and eoL also from line
In [5]:
f=open('poem.txt','w')
f.write(' twinkle twinkle \n')
f.write(' little star \n')
f.write(' what r wonder \n')
f.close()
f=open('poem.txt','r')
lns=f.readlines()
for line in lns:
print(line.lstrip()) #remove only left side space from line
In [6]:
f=open('poem.txt','w')
f.write(' twinkle twinkle \n')
f.write(' little star \n')
f.write(' what r wonder \n')
f.close()
f=open('poem.txt','r')
lns=f.readlines()
for line in lns:
print(line.rstrip()) #remove right side space and EOL also
In [8]:
f=open('poem.txt','w')
f.write(' twinkle twinkle \n')
f.write(' little star \n')
f.write(' what r wonder \n')
f.close()
f=open('poem.txt','r')
lns=f.readlines()
print(lns) #print Lines in List form
In [10]:
f=open('poem.txt','w')
f.write(' twinkle twinkle \n')
f.write(' little star \n')
f.write(' what r wonder \n')
f.close()
f=open('poem.txt','r')
lns=f.read() #read return entire content in one time as found in list
print(lns)
In [11]:
f=open('poem.txt','w')
f.write(' twinkle twinkle \n')
f.write(' little star \n')
f.write(' what r wonder \n')
f.close()
f=open('poem.txt','r')
for line in f:
print(line)
In [12]:
f=open('poem.txt','w')
f.write(' twinkle twinkle \n')
f.write(' little star \n')
f.write(' what r wonder \n')
f.close()
f=open('poem.txt','r')
for line in f:
print(f.readline())
In [14]:
f=open('poem.txt','w')
f.write(' twinkle twinkle \n')
f.write(' little star \n')
f.write(' what r wonder \n')
f.close()
f=open('poem.txt','r')
print(f.read())
In [ ]:
No comments:
Post a Comment