chrisluiz16

Untitled

Jul 24th, 2023
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1.  
  2. ######_________________ Task ____________#####
  3.  
  4.  
  5. file = open('/home/ec2-user/sample.txt','r')
  6. for line in file:
  7. print(line)
  8. file.close()
  9.  
  10. file = open('sample.txt','r')
  11. for line in file:
  12. if 'linux' in line.lower():
  13. print(line)
  14. file.close()
  15.  
  16. file = open('sample.txt','r')
  17. for line in file:
  18. if 'linux' in line.lower():
  19. print(line.lower().replace('linux','<<linux>>'))
  20. file.close()
  21.  
  22. ## N.o of line
  23.  
  24. file = open('sample.txt','r')
  25. nol = 0
  26. for line in file:
  27. nol = nol + 1
  28. print(nol)
  29. file.close()
  30.  
  31. ## N.o of words
  32.  
  33. file = open('sample.txt','r')
  34. now = 0
  35. for item in file:
  36. now += len(item.split())
  37. print(now)
  38. file.close()
  39.  
  40. ## Starts with linux
  41.  
  42. file = open('sample.txt','r')
  43.  
  44. for line in file:
  45.  
  46. if line.lower().startswith('linux'):
  47. print(line)
  48.  
  49. file.close()
  50.  
  51. ## line number
  52.  
  53. file = open('sample.txt','r')
  54. ln = 1
  55.  
  56. for line in file:
  57. print(ln,line)
  58. ln += 1
  59.  
  60. file.close()
  61.  
  62. file = open('sample.txt','r')
  63. ln = 1
  64. for line in file:
  65. if line.lower().startswith('linux'):
  66. print(ln,line)
  67. ln += 1
  68. file.close()
  69.  
  70. ## Strip
  71.  
  72. s1 = ""
  73. s2 = " "
  74. s3 = " dd "
  75. s4 = "---------prince---------------"
  76.  
  77. print(s4.strip("-"))
  78.  
  79. print(s4.lstrip("-"))
  80.  
  81. print(s4.rstrip("-"))
  82.  
  83. print(s1.strip(),s2.strip())
  84.  
  85. print(s3.strip())
  86.  
  87. r1 = len(s1.strip())
  88. r2 = len(s2.strip())
  89. print(r1)
  90. print(r2)
  91.  
  92. ## Empty lines
  93.  
  94. file = open('sample.txt','r')
  95. x = 1
  96.  
  97. for line in file:
  98. if len(line.split()) == 0:
  99. print(x,line)
  100. x += 1
  101. file.close()
  102.  
  103. file = open('sample.txt','r')
  104. x = 1
  105.  
  106. for line in file:
  107. if len(line.split()) != 0:
  108. print(x,line)
  109. x += 1
  110. file.close()
  111.  
  112. file = open('sample.txt','r')
  113. for line in file:
  114. if line.lstrip().rstrip().startswith("#"):
  115. print(line)
  116. file.close()
  117.  
  118. file = open('sample.txt','r')
  119. for line in file:
  120. if not line.lstrip().rstrip().startswith("#"):
  121. print(line)
  122. file.close()
  123.  
  124. ## Write
  125.  
  126. file = open('myfile.txt','w')
  127. file.write("I'am first line")
  128. file.write("I'm second line")
  129. file.write("I'm third line")
  130. file.close()
  131.  
  132. ! cat myfile.txt
  133.  
  134. file = open('myfile.txt','w')
  135. file.write("I'am first line\n")
  136. file.write("I'm second line\n")
  137. file.write("I'm third line\n")
  138. file.close()
  139.  
  140. ! cat myfile.txt
  141.  
  142. fileIn = open('sample.txt','r')
  143. fileOut = open('result.txt','w')
  144.  
  145. for line in fileIn:
  146. if not line.lstrip().rstrip().startswith('#'):
  147. if len(line.strip()) != 0:
  148. fileOut.write(line)
  149. fileOut.close()
  150. fileIn.close()
  151.  
  152. ! cat result.txt
  153.  
  154.  
Advertisement
Add Comment
Please, Sign In to add comment