Python Forum

Full Version: subprocess error : The system cannot find the file specified
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hey all ...

i writing a code to check the windows titles and look for browser google if he found it take a variable from list and run this command on system to bring the full dir for the chrome browser ...

this is my code :
import os
import subprocess

home = os.environ.get("HOMEDRIVE")
lol =home +"/"
#mycurrent = os.getcwd()
#change = os.chdir(lol) 

proc = os.popen("""for /f "tokens=3,*" %a in ('tasklist /fo list /v ^| find "Window Title"') do @if not "%a"=="N/A" echo %a %b""").read().splitlines()
d = os.popen("""wmic process get name | findstr /v "Name 'System Idle Process' System""").read().lower()
#print proc
lists = ["opera.exe" , "chrome.exe" , "iexplore.exe" , "firefox.exe" , "microsoftedgecp.exe"]

if any("Google Chrome" in s for s in proc) and lists[1] in d :
    #home = os.environ.get("HOMEDRIVE")
    #lol =home +"/"
    mycurrent = os.getcwd()
    change = os.chdir(lol)
    mycurrent = os.getcwd()
    print mycurrent
    qassam = subprocess.Popen(["dir /s /b {}"]).format(lists[1])
    #qassam = subprocess.Popen(["dir /s", "/b {}"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT).format(lists)
else:
    pass
but i keep got this error :
Error:
File "c:/Users/root/Desktop/testfunction.py", line 21, in <module> qassam = subprocess.Popen(["dir /s /b {}"]).format(lists[1]) File "C:\Python27\lib\subprocess.py", line 390, in __init__ errread, errwrite) File "C:\Python27\lib\subprocess.py", line 640, in _execute_child startupinfo) WindowsError: [Error 2] The system cannot find the file specified
i try with this 3 methods :

1- qassam = subprocess.Popen(["dir /s /b {}"]).format(lists[1])
2- qassam = subprocess.Popen(["dir /s", "/b {}"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT).format(lists)
3- qassam = os.popen("dir /s /b %s").read() % lists[1]

when i try with the third method i got this error :

Error:
C:\ Traceback (most recent call last): File "c:/Users/root/Desktop/testfunction.py", line 23, in <module> qassam = os.popen("dir /s /b %s").read() % lists[1] TypeError: not all arguments converted during string formatting

solved ..
From my stupidity I forgot to but .format inside ()
i change it from : qassam = os.popen("dir /s /b %s").read() % lists[1] to : qassam = os.popen("dir /s /b {}".format(lists[1])).read()

but i still face this error when i use subporcess :
Error:
File "c:/Users/root/Desktop/testfunction.py", line 21, in <module> qassam = subprocess.Popen(["dir /s /b {}"]).format(lists[1]) File "C:\Python27\lib\subprocess.py", line 390, in __init__ errread, errwrite) File "C:\Python27\lib\subprocess.py", line 640, in _execute_child startupinfo) WindowsError: [Error 2] The system cannot find the file specified