python - 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.
解决方案
我做以下
path = path_d[key]
os.system(r'start "{0}"'.format(path))
所以,用双引号将路径括起来。这样它将处理路径中的空间。如果没有要打开的默认应用程序,它可能会打开命令提示符。所以,如果它是一个文本文件,请执行以下操作
os.system(r'notepad "{0}"'.format(path))
推荐阅读
- php - 在 PHP 中使用可变数量的参数动态调用构造函数
- javascript - 单击链接时,选择 html 表的某些行并将所选行的一列的值发送到另一个 php 页面
- java - 使用美国运通/节点的 GraphQL 查询列表
- javascript - 使用 JavaScript 为 MS Outlook 生成长电子邮件正文
- r - R中两个矩阵(data.frames)列的余弦相似度
- mysql - 匹配 2 个具有特定条件的 SQL 表
- java - 托管到服务器时,登录在 Spring Boot(使用 Spring Security)中不起作用
- aws-lambda - “缺少身份验证令牌”错误,身份验证类型设置为 NONE
- c# - 使用 Vuforia 时,如何在删除目标图像时阻止我的组件显示?
- android - 通过 Util 类绑定数据时模型类中的绑定错误