os.system无法使用左括号'('在文件名中调用文件

问题描述 投票:0回答:2

我正在尝试调用名称中包含括号的可执行文件(例如'test(1).exe')。

使用Python 3.6.2,当我尝试以下操作时:

os.system('test(1).exe')

我明白了:

'test' is not recognized as an internal or external command, operable program or batch file.

但是如果我通过删除'test1).exe'将文件名更改为'(',那么以下工作成功:

os.system('test1).exe')

左括号导致os.system问题的任何想法?

python windows os.system
2个回答
2
投票

在没有subprocess.call()的情况下使用shell=True可以避免以shell安全的方式引用参数:

subprocess.call(["test(1).exe"])

1
投票

解决方案:在'(''前面放一个'^'字符:

os.system('test^(1).exe')
© www.soinside.com 2019 - 2024. All rights reserved.