os.system调用一个位于名称中包含空格的dir中的exe [英] os.system to invoke an exe which lies in a dir whose name contains whitespace
本文介绍了os.system调用一个位于名称中包含空格的dir中的exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码如下:
file = 'C:\\Exe\\First Version\\filename.exe'
os.system(file)
运行此程序时,出现Windows错误:can't find the file specified.
When I run this program, a Windows error is raised: can't find the file specified.
我发现问题出在第一版"中间的空白.我如何找到解决该问题的方法?
I found out the problem has to with the whitespace in the middle of "First Version". How could I find a way to circumvent the problem?
P.S .:如果将变量文件"作为参数传递给另一个函数怎么办?
P.S.: what if the variable 'file' is being passed as an argument into another function?
推荐答案
在路径周围加上引号将起作用:
Putting quotes around the path will work:
file = 'C:\\Exe\\First Version\\filename.exe'
os.system('"' + file + '"')
,但是更好的解决方案是改用subprocess
模块:
but a better solution is to use the subprocess
module instead:
import subprocess
file = 'C:\\Exe\\First Version\\filename.exe'
subprocess.call([file])
这篇关于os.system调用一个位于名称中包含空格的dir中的exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文