September 27, 2018

Python Packages - os subprocess shutil scp pwd zipfile psutil shlex signal argparse

os subprocess shutil scp Packages in Python

import  os
os.system('ls')
os.system('date')
os.system('echo 1 > /proc/sys/net/ipv4/ip_forward')
os.system('scp satya/output.log linux006:/tmp/')
os.rename(current_file_name, new_file_name)
os.remove(file_name)
os.mkdir("/home/ora/11204")
os.makedirs(path[, mode])
os.chdir("/home/ora/11204")
print(os.getcwd())
os.rmdir('dirname')
os.removedirs(path)
os.chmod(path, mode)
os.chown(path, uid, gid)

os.link(src, dst)
os.unlink(path)
os.listdir(path)
os.popen(command[, mode[, bufsize]])
pwd = os.popen('pwd').read()
list = os.popen('ls -l bin/oracle ; ls -l bin/extjobo').read()
fl = os.popen('cat install/relink.log').read()
os.getlogin()
fd = os.open( "/tmp", os.O_RDONLY )
os.fchdir(fd)
dir_path = os.path.dirname(os.path.realpath(__file__))
if not os.path.exists(logdir): os.makedirs(logdir)
filename = os.path.join(directory, prefix)

from os import path
path.exists("filename")
print(path.isfile("output.log"))
path.isdir("filename")
os.path.realpath("filename")
path.gemtime("filename")
sys.path.append(path.abspath(path.join(path.dirname(__file__), '../lib/')))
size = os.stat(file).st_size

import subprocess
from subprocess import call
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)
subprocess.call("command1")
subprocess.call(["command1", "arg1", "arg2"])
subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
time = subprocess.Popen('date', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
hosts=subprocess.Popen(['grep','host:',file_input], stdout= subprocess.PIPE)
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
op = subprocess.Popen(['ps', '-ef'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
op = subprocess.Popen(['ps', '-ef','|', 'grep', 'tns'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
os.system('scp "%s" "%s:%s"' % (localfile, remotehost, remotefile) )
subprocess.Popen(["scp", filename, "%(user)s@%(server)s:%(remotepath)s" % vars]).wait()
Popen.wait()
Popen.terminate()

from subprocess import check_output
subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)
time = subprocess.check_output('date')
hosts = subprocess.check_output("grep 'host:' /root/test.txt")
op = subprocess.check_output('ps -ef | grep tns')
subprocess.check_output(['ls', '|', 'wc', '-l'])
subprocess.check_output(['ps', '-ef','|', 'grep', 'tns'])
print(check_output(["ls", "../input"]).decode("utf8"))
subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, shell=False, timeout=None, check=False, encoding=None, errors=None)

import shutil
from shutil import copyfile
from shutil import make_archive
shutil.copy("file1", "file2")
shutil.copystat("file1", "file2")
shutil.copy2('/src/dir/file.ext', '/dst/dir/newname.ext')
shutil.copyfileobj(fsrc, fdest, buffer_size)
shut.make_archive("files_archive", "zip", dir)

import scp
client = scp.Client(host=host, user=user)
client = scp.Client(host=host, user=user, password=password)
client.transfer('/etc/local/filename', '/etc/remote/filename')
client = scp.Client(host=host, user=user, keyfile=keyfile)

import paramiko
ssh = paramiko.SSHClient()
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
ssh.connect(server, username=username, password=password)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.close()
sftp = ssh.open_sftp()
sftp.put(localpath, remotepath)
from paramiko import SSHClient

from scp import SCPClient
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect('example.com')
with SCPClient(ssh.get_transport()) as scp:
scp.put('test.txt', 'test2.txt')
scp.get('test2.txt')

import pwd
pw_record = pwd.getpwnam(user_name)
uid = pwd.getpwnam('satya')[2]

from zipfile import ZipFile
zfile = zipfile.ZipFile('Test.zip', 'r')
df = zfile.read('train.csv')

import psutil
psutil.Process(os.getpid()).memory_info().rss

import shlex
p = subprocess.Popen(shlex.split(c_args), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
self.command = shlex.split(command)

import signal
signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(0)

import argparse
parser = argparse.ArgumentParser(description='Check number of crond/snmp/ntpd process running')
parser.add_argument('--warning', type=int, metavar='warning', default=30, help='Warning time (minutes) after last run')
args = parser.parse_args()

Related Python Articles:   Dictionaries in Python     String Functions in Python


No comments:

Post a Comment