chrisluiz16

Untitled

Jul 27th, 2023
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. d = {'sam':30, 'kevin':20,'john':40,'ales':10}
  2. ranks = 0
  3.  
  4. for item in d:
  5. rank = d[item]
  6. ranks += rank
  7. print(ranks)
  8.  
  9. dir(d)
  10.  
  11. ##
  12.  
  13. d = {'sam':30, 'kevin':20,'john':40,'ales':10}
  14. ranks = d.values()
  15. print(ranks)
  16.  
  17. dir(ranks)
  18.  
  19. ## d.value()
  20.  
  21. d = {'sam':30, 'kevin':20,'john':40,'ales':10}
  22. ranks = len(d.values())
  23. print(ranks)
  24.  
  25. d = {'sam':30, 'kevin':20,'john':40,'ales':10}
  26. ranks = list(d.values())
  27. print(ranks)
  28.  
  29. d = {'sam':30, 'kevin':20,'john':40,'ales':10}
  30. rank = sum(d.values())
  31. print(rank)
  32.  
  33. ## d.key()
  34.  
  35. d = {'sam':30, 'kevin':20,'john':40,'ales':10}
  36. rank = d.keys()
  37. print(rank)
  38.  
  39. d = {'sam':30, 'kevin':20,'john':40,'ales':10}
  40. rank = list(d.keys())
  41. print(rank)
  42.  
  43. ## string to list
  44.  
  45. name = 'prince'
  46. l = list(name)
  47. print(l)
  48.  
  49. d = {'sam':30, 'kevin':20,'john':40,'ales':10}
  50. rank = list(d)
  51. print(rank)
  52.  
  53. d = {'sam':30, 'kevin':20,'john':40,'ales':10}
  54. rank = list(d.keys())
  55. print(rank)
  56.  
  57. ## tuple to list
  58.  
  59. t = ('sam','kevin','john','alex')
  60. l = list(t)
  61. print(l)
  62.  
  63. ## d.items()
  64.  
  65. d = {'sam':30, 'kevin':20,'john':40,'ales':10}
  66. rank = (d.items())
  67. print(rank)
  68.  
  69. ## Creating tarfile
  70.  
  71. import tarfile
  72.  
  73. file = tarfile.open('/tmp/backup.tar','w')
  74. file.add('/etc/apache2/')
  75. file.add('/etc/resolv.conf')
  76. file.add('/etc/passwd')
  77. file.close()
  78.  
  79. ! ls -lh /tmp
  80.  
  81. import tarfile
  82.  
  83. with tarfile.open('/tmp/backup.tar.gz', 'w:gz') as file:
  84. file.add('/etc/apache2/')
  85. file.add('/etc/resolv.conf')
  86. file.add('/etc/passwd')
  87.  
  88. ! ls -l /tmp/
  89.  
  90. import tarfile
  91.  
  92. with tarfile.open('/tmp/backup.tar.bz2', 'w:bz2') as file:
  93. file.add('/etc/apache2/')
  94. file.add('/etc/resolv.conf')
  95. file.add('/etc/passwd')
  96.  
  97. ! ls -l /tmp/
  98.  
  99. import tarfile
  100.  
  101. with tarfile.open('/tmp/backup.tar.xz', 'w:xz') as file:
  102. file.add('/etc/apache2/')
  103. file.add('/etc/resolv.conf')
  104. file.add('/etc/passwd')
  105.  
  106. ! ls -lh /tmp/
  107.  
  108. import os
  109. import tarfile
  110. import posixpath
  111.  
  112. with tarfile.open('/tmp/apache.conf.tar.bz2','w:bz2') as tar:
  113. for item in os.walk('/etc/apache2/'):
  114. maindir,subdir,files = item
  115. for file in files:
  116. abspath = posixpath.join(maindir,file)
  117. if abspath.endswith('.conf'):
  118. tar.add(abspath)
  119.  
  120. ! tar -tf /tmp/apache.conf.tar.bz2
  121.  
  122. ## Json
  123.  
  124. import json
  125. with open('ip-8.8.8.8.json','r') as file:
  126. output = json.load(file)
  127. print(output)
  128.  
  129. ##
  130.  
  131. import os
  132. import posixpath
  133. import json
  134.  
  135. directory = './'
  136. for file in os.listdir(directory):
  137. if posixpath.isfile(file) and file.endswith('.json'):
  138. with open(file,'r') as x:
  139. output = json.load(x)
  140.  
  141.  
  142. print(output['ip'])
  143. print(output['country_name'])
  144. print(output['currency']['name'])
  145. print(output['isp'])
  146.  
  147. import os
  148. import posixpath
  149. import json
  150.  
  151. directory = './'
  152. for file in os.listdir(directory):
  153. if posixpath.isfile(file) and file.endswith('.json'):
  154. with open(file,'r') as x:
  155. out= json.load(x)
  156.  
  157. print(out['ip'])
  158. print(out['country_name'])
  159. print(out['currency']['name'])
  160. print(out['isp'])
  161. print('--------------------------')
  162.  
  163. import os
  164. import posixpath
  165. import json
  166.  
  167. result = []
  168.  
  169.  
  170. directory = './'
  171. for file in os.listdir(directory):
  172. if posixpath.isfile(file) and file.endswith('.json'):
  173. with open(file,'r') as x:
  174. output = json.load(x)
  175. result.append({'ip':output['ip'], 'country':output['country_name'], 'currency':output['currency']['name'], 'isp':output['isp']})
  176.  
  177. print(result)
  178.  
  179. import json
  180.  
  181. with open('ipgeolocation.json','w') as fp:
  182. json.dump(result,fp,indent=2)
  183.  
  184. ! cat ipgeolocation.json
  185.  
  186.  
Advertisement
Add Comment
Please, Sign In to add comment