chrisluiz16

Untitled

Jul 26th, 2023
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1.  
  2.  
  3. ------------------------------------------------------ Tasks -----------------------------------------------
  4.  
  5. name = "prince"
  6. dir(name)
  7.  
  8. x = 1
  9. dir(x)
  10.  
  11. l = [1,2,3,4,5]
  12. dir(l)
  13.  
  14. l = {"same": 30,"john": 20}
  15. dir(l)
  16.  
  17. l = {"same": 30,"john": 20}
  18. l.update({"chris": 1})
  19. print(l)
  20.  
  21. file = open('access.log','r')
  22. dir(file)
  23.  
  24. ## Access log
  25.  
  26. ##
  27.  
  28. file = open('access.log','r')
  29.  
  30. count = 1
  31. for line in file:
  32. if count == 10:
  33. print(line)
  34. break
  35. count += 1
  36. file.close()
  37.  
  38. ## Advanced file opening method
  39.  
  40. count = 1
  41. with open('access.log','r') as file:
  42. for line in file:
  43. if count == 10:
  44. print(line)
  45. break
  46. else:
  47. count += 1
  48.  
  49. ## Tuple
  50.  
  51. persons = ['sam','devon','john','kevin']
  52. persons.append("chris")
  53. print(persons)
  54.  
  55. dir(persons)
  56.  
  57. print("before -", persons)
  58. persons.insert(1,'prince')
  59. print(persons)
  60.  
  61. ##
  62.  
  63. persons[1] = "vysakh"
  64. print(persons)
  65.  
  66. persons.remove('devon')
  67. print(persons)
  68.  
  69. x = persons.pop(3)
  70. print(x)
  71.  
  72. ##
  73.  
  74. t = ('sam', 'vysakh', 'sam','john', 'kevin',1,2,3,4)
  75. print(t.count('sam'))
  76. print(t.index('sam'))
  77.  
  78. ##
  79.  
  80. t = ('sam',['vysakh', 'sam'],{'john': 20, 'kevin': 30})
  81. print(len(t))
  82.  
  83. print(t[0])
  84.  
  85. print(t[1][0])
  86.  
  87. print (len(t[1]))
  88.  
  89. print(len(t[-1]))
  90.  
  91. t[1].append('prince')
  92.  
  93. print(t)
  94.  
  95. t[-1].update({'chris' : 50})
  96. print(t)
  97.  
  98. ## Directory listing
  99.  
  100. import os
  101. print(os.listdir('/etc/apache2/'))
  102.  
  103. ! tree /etc/apache2/
  104.  
  105. ##
  106.  
  107. import os
  108. import posixpath
  109. directory = os.walk('/etc/apache2/')
  110. for item in directory:
  111. maindir,subdir,files = item
  112. for file in files:
  113. abspath = posixpath.join(maindir,file)
  114. print(abspath)
  115.  
  116.  
  117. ## conf extension files
  118.  
  119. import os
  120. import posixpath
  121. directory = os.walk('/etc/apache2/')
  122. for item in directory:
  123. maindir,subdir,files = item
  124. for file in files:
  125. abspath = posixpath.join(maindir,file)
  126. if abspath.endswith('.conf'):
  127. print(abspath)
  128.  
  129.  
  130. ## Total size in conf
  131.  
  132. import os
  133. import posixpath
  134. directory = os.walk('/etc/apache2/')
  135. for item in directory:
  136. maindir,subdir,files = item
  137. for file in files:
  138. abspath = posixpath.join(maindir,file)
  139. if abspath.endswith('.conf'):
  140. size = posixpath.getsize(abspath)
  141. out = "{} - {}".format(abspath,size)
  142. print(out)
  143.  
  144. ## Size = zero
  145.  
  146. import os
  147. import posixpath
  148. directory = os.walk('/etc/apache2/')
  149. for item in directory:
  150. maindir,subdir,files = item
  151. for file in files:
  152. abspath = posixpath.join(maindir,file)
  153. size = posixpath.getsize(abspath)
  154. if size == 0:
  155. print(abspath,size)
  156.  
  157. ## shutil
  158.  
  159. import shutil
  160. shutil.copy('access.log','/home/ec2-user/')
  161.  
  162. ! ls /home/ec2-user/
  163.  
  164. ## Copying confs to a new directory
  165.  
  166. import os
  167. import posixpath
  168. import shutil
  169.  
  170. directory = os.walk('/etc/apache2/conf-available/')
  171.  
  172. for item in directory:
  173. maindir,subdir,files = item
  174. for file in files:
  175. abspath = posixpath.join(maindir,file)
  176. if not os.path.exists('./backup'):
  177. os.makedirs('./backup')
  178. print(os.listdir('./backup'))
  179. shutil.copy(abspath,'./backup/')
  180. print(os.listdir('./backup'))
  181.  
  182. ## Copying extensions
  183.  
  184. import os
  185. import posixpath
  186. import shutil
  187.  
  188. directory = os.walk('/home/ec2-user/backup')
  189.  
  190. for item in directory:
  191. maindir,subdir,files = item
  192. for file in files:
  193. abspath = posixpath.join(maindir,file)
  194. *_,x = posixpath.splitext(abspath)
  195. ext = x.lstrip(".")
  196. print(ext)
  197. break
  198. break
  199.  
  200. ##
  201. import os
  202. import posixpath
  203. import shutil
  204. mydict = {}
  205.  
  206. directory = os.walk('/home/ec2-user/backup')
  207.  
  208. for item in directory:
  209. maindir,subdir,files = item
  210. for file in files:
  211. abspath = posixpath.join(maindir,file)
  212. *_,x = posixpath.splitext(abspath)
  213. ext = x.lstrip(".")
  214. if ext not in mydict:
  215. mydict[ext] = [abspath]
  216. else:
  217. mydict[ext].append(abspath)
  218. print(mydict)
  219.  
  220. print(mydict['mov'])
  221.  
  222. ! mkdir -p /home/ec2-user/backup/english/old && cd /home/ec2-user/backup/english/old && touch movies-old{1..5}.mov && touch movies-new{1..5}.mp4
  223.  
  224.  
  225. print(mydict['mov'])
  226.  
  227. ## Additional directories
  228.  
  229. import os
  230. import posixpath
  231. import shutil
  232. mydict = {}
  233.  
  234. directory = os.walk('/home/ec2-user/backup')
  235. root = '/home/ec2-user/'
  236.  
  237. for item in directory:
  238. maindir,subdir,files = item
  239. for file in files:
  240. abspath = posixpath.join(maindir,file)
  241. *_,x = posixpath.splitext(abspath)
  242. ext = x.lstrip(".")
  243. new = posixpath.join(root,ext)
  244. if not os.path.exists(new):
  245. os.mkdir(new)
  246. if abspath.endswith(ext):
  247. shutil.copy(abspath,(posixpath.join(root,ext)))
  248.  
  249.  
  250.  
Advertisement
Add Comment
Please, Sign In to add comment