i am trying to get svchost.exe path by PID using python. i tried it using psutil but i got access denied error.
here is my code-
import psutil
p = psutil.Process(1832)
print p.exe()
you can get process path using wmi module.
Here is a sample code for you. This code fids all process named svchost and return path to the process. This returns full information on process.
import wmi
import psutil
c = wmi.WMI ()
process = psutil.Process(2276)
process_name = process.name()
for process in c.Win32_Process(name=process_name):
if process.ExecutablePath:
print (process.ExecutablePath)
Output
c:\windows\system32\svchost.exe
c:\windows\system32\svchost.exe
c:\windows\system32\svchost.exe
Related
import subprocess
import os
....
....
opn=open(ak,'w')
tt=txt.get(1.0,END)
opn.write(tt)
lst=[]
for i in range(0,len(ak)):
if ak[i]=='/':
lst.append(i)
else:
pass
val=lst[-1]+1
path=ak
file_name=path[val:]
sudo_path_name=path[3:val-1]
dir_name=path[:2]
path_name="cd "
for i in sudo_path_name:
if i=='/':
path_name+='\\'
else:
path_name+=i
command=dir_name+'&&'+path_name+'&&'+file_name
os.system(command)
output = subprocess.getoutput(command)
print(output)
ak is path of a file
My aim is just print the output when ak execute..
But whenever I tried to execute it give output as
The system cannot find the path specified.
The system cannot find the path specified.
When i get command and run with commmand prompt it executes successfully with no error..
Thank You
This depends on the editor. It happened to me too I used VS-code but when I changed cwd it worked! So it may be that problem.
Also try putting the full path or try this code:-
from pathlib import Path
import os
os.chdir(Path(__file__).parent)
Currently I have a program that uses subprocess.Popen to open an executable file and pass an argument - equivalent to running ./path/to/file args on Linux.
This works very well but I have to execute this file over 1000 times and currently it is done one at a time, on a single processor. I want to be able to execute this file in sets of 8 for example, as I have an 8-core PC.
I have tried the following:
bolsig = ("/home/rdoyle/TEST_PROC/BOLSIG/bolsigminus")
infile_list = glob.glob(str(cwd)+"/BOLSIG Run Files/run*")
cmds_list = [[bolsig, infile] for infile in infile_list]
procs_list = [Popen(cmd) for cmd in cmds_list]
for proc in procs_list:
proc.wait()
But this tries to execute all 1000 commands at the same time.
Anyone have any suggestions?
I like concurrent.futures for simple cases like this, it's so simple and yet so effective.
import os
import glob
from concurrent import futures
from subprocess import Popen
optim = ("/usr/bin/jpegoptim")
img_path = os.path.join(os.path.abspath(os.path.curdir), 'images')
file_list = glob.glob(img_path+'/*jpg')
def compress(fname):
Popen([optim, fname, '-d', 'out/', '-f'])
ex = futures.ThreadPoolExecutor(max_workers=8)
ex.map(compress, file_list)
A great intro at Doug Hellman's PyMOTW. https://pymotw.com/3/concurrent.futures/
You can use the Python multiprocessing module and its multiprocessing.pool.Pool -> https://docs.python.org/3/library/multiprocessing.html#module-multiprocessing
I have script in remote device and I want to run specific function in python script in remote device
remote device has below script:
#connect.py
class ConnectDevice:
def __init__(self, ip):
connect.Device(ip)
def get_devicestate(self):
state = show.Device_State
return state
configured password less connection from source machine to remote machine
function get_devicestate return up or down.
How to get get_devicestate output from source machine. source machine has below script:
import os
import sys
import time
import getpass
import errno
import json
import subprocess
import threading
from subprocess import call
from subprocess import check_output
call(["ssh", "1.1.1.1", "\"python Connect.py\""])#This is just example how to run script from source to remote. Need help how to run function get_devicestate and get value.
At a first glance , it seems that connect.py has got more code than you have pasted in your question. Anyways, assuming connect.py does not require any input parameters to run, simply use subprocess's check_output method to get the stdout message and store it in a variable for further use.
from subprocess import check_output
out = check_output(["ssh", "1.1.1.1", "\"python Connect.py\""])
I am trying to make a gui that displays a path to a file, and the user can change it anytime. I have my defaults which are in my first script.The following is a simplified version without any of the gui stuff. But then the user pushes a button and it runs a different script (script2). In this script, the information on the file is read.
script1:
import os
import multiprocessing as mp
import script2
specsfile = mp.Array('c',1000, lock=True)
path_save = mp.Array('c',1000, lock=True)
p = mp.Process(target=script2, args=(specsfile,path_save))
p.start()
specsfile = '//_an_excel_sheet_directory.xlsx'
path_save = '//path/to/my/directory/'
subprocess.call([sys.executable, 'script2.py'])
script2:
import multiprocessing as mp
from script1 import specsfile
from script1 import path_save
print(specsfile)
spec= pd.read_excel(specsfile)
When I run it, it gives me this error: PermissionError: [WinError 5] Access is denied
I'm not sure if I'm initializing this wrong or not. I've never used multiprocessing but I was reading about some recommendations for that when sharing data. so basically I want to initialize a specsfile string and a path_save string but when it changes,I want it to be reflected and sent to specs2 file.
I know how to use wmi, I have used it before, however, the wmi class it seems i need to call is GetSystemPowerStatus. but i am having trouble finding and documentation on it. to be able to access it, i need to know the namespace, and the format of the data inside the class. could someone help me? Also some sample code would be nice.
Using ctypes, you can call win32 api:
from ctypes import *
class PowerClass(Structure):
_fields_ = [('ACLineStatus', c_byte),
('BatteryFlag', c_byte),
('BatteryLifePercent', c_byte),
('Reserved1',c_byte),
('BatteryLifeTime',c_ulong),
('BatteryFullLifeTime',c_ulong)]
powerclass = PowerClass()
result = windll.kernel32.GetSystemPowerStatus(byref(powerclass))
print(powerclass.BatteryLifePercent)
Above code comes from here.
Using Win32_Battery class (You need to install pywin32):
from win32com.client import GetObject
WMI = GetObject('winmgmts:')
for battery in WMI.InstancesOf('Win32_Battery'):
print(battery.EstimatedChargeRemaining)
Alternative that use wmi package:
import wmi
w = wmi.WMI()
for battery in w.query('select * from Win32_Battery'):
print battery.EstimatedChargeRemaining
```
import subprocess
wmic = subprocess.getoutput("wmic path win32_battery get EstimatedChargeRemaining")
print(wmic)
```
output:
EstimatedChargeRemaining
96