Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ######_________________ Task ____________#####
- file = open('/home/ec2-user/sample.txt','r')
- for line in file:
- print(line)
- file.close()
- file = open('sample.txt','r')
- for line in file:
- if 'linux' in line.lower():
- print(line)
- file.close()
- file = open('sample.txt','r')
- for line in file:
- if 'linux' in line.lower():
- print(line.lower().replace('linux','<<linux>>'))
- file.close()
- ## N.o of line
- file = open('sample.txt','r')
- nol = 0
- for line in file:
- nol = nol + 1
- print(nol)
- file.close()
- ## N.o of words
- file = open('sample.txt','r')
- now = 0
- for item in file:
- now += len(item.split())
- print(now)
- file.close()
- ## Starts with linux
- file = open('sample.txt','r')
- for line in file:
- if line.lower().startswith('linux'):
- print(line)
- file.close()
- ## line number
- file = open('sample.txt','r')
- ln = 1
- for line in file:
- print(ln,line)
- ln += 1
- file.close()
- file = open('sample.txt','r')
- ln = 1
- for line in file:
- if line.lower().startswith('linux'):
- print(ln,line)
- ln += 1
- file.close()
- ## Strip
- s1 = ""
- s2 = " "
- s3 = " dd "
- s4 = "---------prince---------------"
- print(s4.strip("-"))
- print(s4.lstrip("-"))
- print(s4.rstrip("-"))
- print(s1.strip(),s2.strip())
- print(s3.strip())
- r1 = len(s1.strip())
- r2 = len(s2.strip())
- print(r1)
- print(r2)
- ## Empty lines
- file = open('sample.txt','r')
- x = 1
- for line in file:
- if len(line.split()) == 0:
- print(x,line)
- x += 1
- file.close()
- file = open('sample.txt','r')
- x = 1
- for line in file:
- if len(line.split()) != 0:
- print(x,line)
- x += 1
- file.close()
- file = open('sample.txt','r')
- for line in file:
- if line.lstrip().rstrip().startswith("#"):
- print(line)
- file.close()
- file = open('sample.txt','r')
- for line in file:
- if not line.lstrip().rstrip().startswith("#"):
- print(line)
- file.close()
- ## Write
- file = open('myfile.txt','w')
- file.write("I'am first line")
- file.write("I'm second line")
- file.write("I'm third line")
- file.close()
- ! cat myfile.txt
- file = open('myfile.txt','w')
- file.write("I'am first line\n")
- file.write("I'm second line\n")
- file.write("I'm third line\n")
- file.close()
- ! cat myfile.txt
- fileIn = open('sample.txt','r')
- fileOut = open('result.txt','w')
- for line in fileIn:
- if not line.lstrip().rstrip().startswith('#'):
- if len(line.strip()) != 0:
- fileOut.write(line)
- fileOut.close()
- fileIn.close()
- ! cat result.txt
Advertisement
Add Comment
Please, Sign In to add comment