Python Forum
subprocess call cannot find the file specified
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
subprocess call cannot find the file specified
#1
Hi,
I am trying to launch a batch file in windows with some arguments from a different directory.
Well its working with
os.system
call, as its a straight forward way.
But i want to use
subprocess.call
in order to have better control of result.

If i use command
Quote: test.bat -file_arg=drawings/mydrawings.cfg
it works, but through python program its gives the error.

Please let me know what i am missing

Output:
C:\Users\Projects>C:\Python27\python.exe fp_subprocess.py CWD: C:\Users\Projects BAT DIR C:\\MyWork\\Temp\\ script DIR test.bat -file=drawings/mydrawings.cfg Traceback (most recent call last): File "fp_subprocess.py", line 28, in <module> if True == launch_BAT(): File "fp_subprocess.py", line 14, in launch_BAT subprocess.call(["test.bat"+" -file="+TESTCFG_PATH]) File "C:\Python27\lib\subprocess.py", line 172, in call return Popen(*popenargs, **kwargs).wait() File "C:\Python27\lib\subprocess.py", line 394, in __init__ errread, errwrite) File "C:\Python27\lib\subprocess.py", line 644, in _execute_child startupinfo) WindowsError: [Error 2] The system cannot find the file specified
def launch_BAT():
	TESTBAT_PATH = b'C:\\MyWork\\Temp\\'
	TESTCFG_PATH = b'drawings/mydrawings.cfg'# also tried 'drawings\\mydrawings.cfg'
	print "CWD:", os.getcwd()
	os.chdir(TESTBAT_PATH)
	print "BAT DIR", os.getcwd()
	print "script DIR", "test.bat"+" -file="+TESTCFG_PATH
	print
	
	subprocess.call(["test.bat"+" -file="+TESTCFG_PATH])
	
if __name__ == "__main__":
	if True == launch_BAT():
		print "**launch_BAT Successful**"
	else:
		print "**launch_BAT UnSuccessful**"
Reply
#2
Instead of changing the CWD why not just pass fullpath?
No need to conatenate str in subprocess call

import os
def launch_BAT():
    TESTBAT_PATH = r'C:\MyWork\Temp\'
    TESTCFG_PATH = r'drawings\mydrawings.cfg'
    subprocess.run(["test.bat", f"-file={os.path.join(TESTBAT_PATH, TESTCFG_PATH)}"])
* code not tested
Note that your function will return None, so True/False checks you do will not work and in any case (assuming at some point the function start to return something else) if launch_BAT(): is better than comparing with True - True == launch_BAT().

Also it's better if your function has params and you pass paths as arguments, instead of having hardcoded paths.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Oct-15-2020, 09:22 AM)buran Wrote: Instead of changing the CWD why not just pass fullpath?
No, the script is designed in such a way that it accepts only relative path, it does not work if we pass full path.
(Oct-15-2020, 09:22 AM)buran Wrote: Also it's better if your function has params and you pass paths as arguments, instead of having hardcoded paths.
Its just a minimal code to reproduce the problem.
Reply
#4
by the way I just now noticed you are using python 2.7. Some of changes I made will not work for version prior 3.6
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Oct-15-2020, 10:12 AM)buran Wrote: by the way I just now noticed you are using python 2.7. Some of changes I made will not work for version prior 3.6

Yes its giving Invalid synatax at the last double quote in
subprocess.call
How can i safety launch .bat file as per my requirement.
Reply
#6
Use the cwd input on your sub-process method.

subprocess.run("exit 1", shell=True, check=True, cwd='./directory-here')
check out the docs for full info here: https://docs.python.org/3/library/subprocess.html

Note that call is an old/retired function, if you're on Python 3 or higher you probably want to use .run instead.
Reply
#7
(Oct-15-2020, 10:44 AM)yaythomas Wrote: Use the cwd input on your sub-process method.

subprocess.run("exit 1", shell=True, check=True, cwd='./directory-here')
check out the docs for full info here: https://docs.python.org/3/library/subprocess.html

Note that call is an old/retired function, if you're on Python 3 or higher you probably want to use .run instead.

Nooo yaythomas,

please give me something which works on Python 2.7
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Absolute paths in subprocess - file not found kittyticker 4 406 Jan-28-2024, 10:37 PM
Last Post: kittyticker
  FileNotFoundError: [WinError 2] The system cannot find the file specified NewBiee 2 1,496 Jul-31-2023, 11:42 AM
Last Post: deanhystad
  Cannot find py credentials file standenman 5 1,572 Feb-25-2023, 08:30 PM
Last Post: Jeff900
  selenium can't find a file in my desk ? SouAmego22 0 702 Feb-14-2023, 03:21 PM
Last Post: SouAmego22
  Find (each) element from a list in a file tester_V 3 1,157 Nov-15-2022, 08:40 PM
Last Post: tester_V
  what will be the best way to find data in txt file? korenron 2 1,129 Jul-25-2022, 10:03 AM
Last Post: korenron
  find some word in text list file and a bit change to them RolanRoll 3 1,482 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  Find and delete above a certain line in text file cubangt 12 3,355 Mar-18-2022, 07:49 PM
Last Post: snippsat
Question Help to find the largest int number in a file directory SalzmannNicholas 1 1,588 Jan-13-2022, 05:22 PM
Last Post: ndc85430
  How to run a particular file in subprocess Shiri 5 1,706 Nov-24-2021, 09:29 AM
Last Post: ghoul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020