Sunday, July 12, 2020

Python file Handling ,strip(),lstrip(),rstrip(),read() and readline() function usages and Difference Class 12


Python Text File Function Strip(),lstrip(),rstrip(),read,readline(),readliines
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 
twinkle twinkle
little star
what r wonder
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 
twinkle twinklelittle starwhat r wonder
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
twinkle twinkle    

little star  

what r wonder   

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
    twinkle twinkle
   little star
  what r wonder
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 
['    twinkle twinkle    \n', '   little star  \n', '  what r wonder   \n']
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)  
    twinkle twinkle    
   little star  
  what r wonder   

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)
    twinkle twinkle    

   little star  

  what r wonder   

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())
   little star  


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())
    twinkle twinkle    
   little star  
  what r wonder   

In [ ]:
 

No comments:

Post a Comment