问题
first time asking a question here. What I am trying to do is launch an executable with os.system after the exe path has been assigned to a variable, and having os.system open the path assigned to the variable. It works fine if I have just the path pasted in the parenthesis (with the quotes ofc) but when I only have the variable there, it does not launch anything. I have tried the print function on the same variable and it prints the path out correctly. Here is what I have that creates the variable and then the call.
config = open("config.txt")
lines=config.readlines()
appone = lines[0]
def launchappone():
os.system(appone)
I have even put the quotes on the text in the config file I have, but still no dice. Any help? Thanks.
回答1:
Nowadays, you should use the standard libraries subprocess
module to do such tasks.
Also, you should always use context managers with files. These handle automical closing and exception handling.
What might also be a problem, is that readlines()
will return all lines in the file as a list but with endline character.
Use f.read().splitlines()
to remove the endline or call .strip()
on the individual lines.
putting it together:
import subprocess as sp
with open('config.txt') as config:
lines = config.read().splitlines()
appone = lines[0]
def launch_appone():
sp.run([appone])
Edit: also the python docs mention that os.system should not be used anymore
The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes.
https://docs.python.org/3/library/os.html#os.system
回答2:
If you just need it to execute you could try using call from the subprocess module
from subprocess import call
call([appone])
回答3:
your problem could be with the variable scope also you should use context managers like @MaxNoe said.
but for now try putting argument to the function.
config = open("config.txt")
lines=config.readlines()
appone = lines[0]
def launchappone(appone):
os.system(appone)
回答4:
your code worked if launchappone() method called
import os
config = open("config.txt")
lines=config.readlines()
appone = lines[0]
def launchappone():
os.system(appone)
launchappone() # calling method
回答5:
I've been having some trouble with os.system and found something very odd that might be the issue here. It seems like os.system removes the first and last quote in the command string.
To test I make a batch file named test.bat with the single command:
echo %*
I put it into c:\Test Folder
and I am obviously on Windows, I don't know if this issue would carry to other OS's. Also to note I have been working in Python 2.7.x, so this might be different in Python 3 or even newer patches of 2.7 (I think the newest I've tried is 2.7.6).
I run python and then try some basic commands.
import os
os.system('c:\Test Folder\test.bat') 'test.bat" "Hello"' is not recognized as an internal or external command, operable program or batch file.
Fails as expected, the path has a space in it and there are no quotes. Also, the backslashes should be escaped (or replaced with forward slashes).
os.system('"c:\Test Folder\test.bat"') ECHO is on.
This works. I can add a parameter and get the echo back:
os.system('"c:\Test Folder\test.bat" Hello') Hello
But if I quote the parameter it breaks:
os.system('"c:\Test Folder\test.bat" "Hello"') 'test.bat" "Hello"' is not recognized as an internal or external command, operable program or batch file.
I tried all sorts of stuff and then noticed that the double quote at the start is missing. So I put an extra one in and I get:
os.system('""c:\Test Folder\test.bat" "Hello"') "Hello
So that worked, but the output is also missing the closing quote. So I put an extra closing quote:
os.system('""c:\Test Folder\test.bat" "Hello""') "Hello"
(Please note none of the above changes if you escape the double quotes or not.)
So...I don't know why this is happening, but hey, there you are.
来源:https://stackoverflow.com/questions/43506784/use-os-system-to-launch-executable-from-path-assigned-to-a-variable