让我们假设这是一个使用python脚本获取的文件路径:
f_path = os.getcwd() + "\\" +'out.dat'
此变量的值例如是:
'C:\\My Python\\output\\out.dat'
现在我想通过cmd使用外部程序(extProg)打开此文件。像这样的东西:
cmd_to_parse = 'extProg ' + f_path
os.system(cmd_to_parse)
我收到了以下错误:
couldn't read file "E:\My": no such file or directory
这可能是由于文件路径中出现双反斜杠。我该如何解决这个问题?
答案 0 :(得分:1)
在带有空格'"C:\\My Python\\output\\out.dat"'
的路径周围使用引号,例如:
cmd_to_parse = 'extProg ' + '"' + f_path + '"'