首页 > 解决方案 > Python如何在通过格式化程序时用空格打开文件路径

问题描述

如何在使用start命令时运行需要包含空格的文件路径的命令os.system

例如:

# path_d[key] = C:\Users\John\Documents\Some File With Space.exe
path = path_d[key]
os.system("start {0}".format(path))

当我尝试运行它时,我最终收到一条错误消息:

Windows cannot find 'C:\Users\John\Documents\Some.'. Make sure you typed the name correctly, and then try again.

标签: python

解决方案


我做以下

path = path_d[key]
os.system(r'start "{0}"'.format(path))

所以,用双引号将路径括起来。这样它将处理路径中的空间。如果没有要打开的默认应用程序,它可能会打开命令提示符。所以,如果它是一个文本文件,请执行以下操作

os.system(r'notepad "{0}"'.format(path))

推荐阅读