简体   繁体   English

Python(在 Win10 上):将“\”替换为“/”的路径问题

[英]Python(on Win10): Path issue replacing “\” with “/”

Preface: I am a beginner to Python前言:我是Python的初学者

Problem statement: I am writing a script wherein I will be launching an application ( Gotit.exe ) sitting at particular path lets say D:\Some Folder\SomeMore Folder\AgainFolder\myPythonFolder\Gotit.exe .问题陈述:我正在编写一个脚本,其中我将启动一个位于特定路径的应用程序( Gotit.exe )让我们说D:\Some Folder\SomeMore Folder\AgainFolder\myPythonFolder\Gotit.exe I have kept the python-script also in myPythonFolder .我也将 python-script 保存在myPythonFolder中。

I am accessing the folder path via os.path.dirname(os.path.realpath(__file__)) and selecting particular application by appending it with \Gotit.exe but when passing the same appended string stored in a variable ie GotitexePath to os.system(GotitexePath) its throwing error as,我正在通过os.path.dirname(os.path.realpath(__file__))访问文件夹路径,并通过将其附加到\Gotit.exe来选择特定应用程序,但是当将存储在变量中的相同附加字符串(即GotitexePathos.system(GotitexePath)其抛出错误为,

'D:\Some ' is not recognized as an internal or external command, operable program or batch file.** 'D:\Some' 不是内部或外部命令、可运行程序或批处理文件。**

Kindly help me out to solve the said issue请帮我解决上述问题

I am using python 3.8.2 on Win10 Machine我在 Win10 机器上使用 python 3.8.2

The error is pointing to Some Folder name.错误指向Some Folder名称。 Since there is a space in path you provide, the system doesn't know whether it is a part of folder name or it is a next argument to the command.由于您提供的路径中有空格,因此系统不知道它是文件夹名称的一部分还是命令的下一个参数。

You need to escape the blank space.您需要转义空格。 There are multiple ways to to it.有多种方法可以实现。 For example wrap the path with double quotes:例如用双引号包裹路径:

"D:\Some Folder\SomeMore Folder\AgainFolder\myPythonFolder\Gotit.exe"

For more ways see this post更多方法见这篇文章

os.system("\"%s\"" % GotitexePath)

A the previous replies say, you need to add additional quotation marks around the path for the windows command line.前面的回复说,您需要在 windows 命令行的路径周围添加额外的引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM