使用Python在路径中运行包含空格的应用程序

时间:2017-03-24 09:04:12

标签: python-2.7

我想运行一个用户将在文件中指定的应用程序,它的路径可能包含也可能不包含空格。以下是我的代码。

command1 ='\''+'"'+DIRECTORY+'"'+'\''
command = '"C:/Program Files (x86)/somepath/inside_somepath/Application_name.EXE"'
# Checking for difference
print [i for i in xrange(len(command)) if command1[i] !=command[i]]
if (command1==command):
    os.system ("start /min \"\" " + command)
    print "Equal"
else:
    print "Not Equal"

#DIRECTORY is a variable taken from a file . In that File I have written only one line & 
#that is : C:/Program Files (x86)/somepath/inside_somepath/Application_name.EXE

当我运行os.system ("start /min \"\" " + command)时,它会打开应用程序,但是当我运行os.system ("start /min \"\" " + command1)时。它不会

理想情况下,command1和amp;命令应该是相同的,但是当我比较时,我得到这个结果

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 
38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 52, 53, 54, 55, 56, 
57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69]

Not Equal

另请注意,我删除了字符' \ n'当我从文件中读取数据时。有人可以解释一下为什么它们似乎与python不同..提前感谢

1 个答案:

答案 0 :(得分:1)

command1command不相等,command1被''(撇号或单引号)字符包围。 尝试打印command1command以查看路径字符串

command1 = '\'' + '"'+DIRECTORY+'"' + '\''
command  = '"C:/Program Files (x86)/somepath/inside_somepath/Application_name.EXE"'

print command1
print command

我怀疑你正在尝试这样做

command1 = '"' + DIRECTORY + '"'
command  = '"C:/Program Files (x86)/somepath/inside_somepath/Application_name.EXE"'