Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------------------------------------------------ Tasks -----------------------------------------------
- name = "prince"
- dir(name)
- x = 1
- dir(x)
- l = [1,2,3,4,5]
- dir(l)
- l = {"same": 30,"john": 20}
- dir(l)
- l = {"same": 30,"john": 20}
- l.update({"chris": 1})
- print(l)
- file = open('access.log','r')
- dir(file)
- ## Access log
- ##
- file = open('access.log','r')
- count = 1
- for line in file:
- if count == 10:
- print(line)
- break
- count += 1
- file.close()
- ## Advanced file opening method
- count = 1
- with open('access.log','r') as file:
- for line in file:
- if count == 10:
- print(line)
- break
- else:
- count += 1
- ## Tuple
- persons = ['sam','devon','john','kevin']
- persons.append("chris")
- print(persons)
- dir(persons)
- print("before -", persons)
- persons.insert(1,'prince')
- print(persons)
- ##
- persons[1] = "vysakh"
- print(persons)
- persons.remove('devon')
- print(persons)
- x = persons.pop(3)
- print(x)
- ##
- t = ('sam', 'vysakh', 'sam','john', 'kevin',1,2,3,4)
- print(t.count('sam'))
- print(t.index('sam'))
- ##
- t = ('sam',['vysakh', 'sam'],{'john': 20, 'kevin': 30})
- print(len(t))
- print(t[0])
- print(t[1][0])
- print (len(t[1]))
- print(len(t[-1]))
- t[1].append('prince')
- print(t)
- t[-1].update({'chris' : 50})
- print(t)
- ## Directory listing
- import os
- print(os.listdir('/etc/apache2/'))
- ! tree /etc/apache2/
- ##
- import os
- import posixpath
- directory = os.walk('/etc/apache2/')
- for item in directory:
- maindir,subdir,files = item
- for file in files:
- abspath = posixpath.join(maindir,file)
- print(abspath)
- ## conf extension files
- import os
- import posixpath
- directory = os.walk('/etc/apache2/')
- for item in directory:
- maindir,subdir,files = item
- for file in files:
- abspath = posixpath.join(maindir,file)
- if abspath.endswith('.conf'):
- print(abspath)
- ## Total size in conf
- import os
- import posixpath
- directory = os.walk('/etc/apache2/')
- for item in directory:
- maindir,subdir,files = item
- for file in files:
- abspath = posixpath.join(maindir,file)
- if abspath.endswith('.conf'):
- size = posixpath.getsize(abspath)
- out = "{} - {}".format(abspath,size)
- print(out)
- ## Size = zero
- import os
- import posixpath
- directory = os.walk('/etc/apache2/')
- for item in directory:
- maindir,subdir,files = item
- for file in files:
- abspath = posixpath.join(maindir,file)
- size = posixpath.getsize(abspath)
- if size == 0:
- print(abspath,size)
- ## shutil
- import shutil
- shutil.copy('access.log','/home/ec2-user/')
- ! ls /home/ec2-user/
- ## Copying confs to a new directory
- import os
- import posixpath
- import shutil
- directory = os.walk('/etc/apache2/conf-available/')
- for item in directory:
- maindir,subdir,files = item
- for file in files:
- abspath = posixpath.join(maindir,file)
- if not os.path.exists('./backup'):
- os.makedirs('./backup')
- print(os.listdir('./backup'))
- shutil.copy(abspath,'./backup/')
- print(os.listdir('./backup'))
- ## Copying extensions
- import os
- import posixpath
- import shutil
- directory = os.walk('/home/ec2-user/backup')
- for item in directory:
- maindir,subdir,files = item
- for file in files:
- abspath = posixpath.join(maindir,file)
- *_,x = posixpath.splitext(abspath)
- ext = x.lstrip(".")
- print(ext)
- break
- break
- ##
- import os
- import posixpath
- import shutil
- mydict = {}
- directory = os.walk('/home/ec2-user/backup')
- for item in directory:
- maindir,subdir,files = item
- for file in files:
- abspath = posixpath.join(maindir,file)
- *_,x = posixpath.splitext(abspath)
- ext = x.lstrip(".")
- if ext not in mydict:
- mydict[ext] = [abspath]
- else:
- mydict[ext].append(abspath)
- print(mydict)
- print(mydict['mov'])
- ! 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
- print(mydict['mov'])
- ## Additional directories
- import os
- import posixpath
- import shutil
- mydict = {}
- directory = os.walk('/home/ec2-user/backup')
- root = '/home/ec2-user/'
- for item in directory:
- maindir,subdir,files = item
- for file in files:
- abspath = posixpath.join(maindir,file)
- *_,x = posixpath.splitext(abspath)
- ext = x.lstrip(".")
- new = posixpath.join(root,ext)
- if not os.path.exists(new):
- os.mkdir(new)
- if abspath.endswith(ext):
- shutil.copy(abspath,(posixpath.join(root,ext)))
Advertisement
Add Comment
Please, Sign In to add comment