为什么这个 dos 命令在 python 中不起作用? [英] why this dos command does not work inside python?

查看:46
本文介绍了为什么这个 dos 命令在 python 中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将一些 dos 命令从我的批处理文件移动到 python 中,但收到此错误,文件名、目录名或卷标语法不正确,对于以下语句.

I try to move some dos command from my batch file into python but get this error, The filename, directory name, or volume label syntax is incorrect, for the following statement.

subprocess.Popen('rd /s /q .\ProcessControlSimulator\bin', shell=True, 
                  stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

如果我只是将该 dos 命令复制到窗口控制台中,它就可以工作.os.getcwd() 给了我预期的工作目录.

if I just copy that dos command into window console, it works. The os.getcwd() gave me expected working directory.

我的问题是:1.这是为什么?2.如何避免?我是否需要获取当前工作目录并为该命令构建一个抽象路径?怎么做?

My questions are: 1. why is that? 2. how to avoid that? do I need to get current working directory and construct an abstract path for that command? how to do that?

谢谢

推荐答案

\(反斜杠)是字符串常量中的转义字符,因此您的字符串最终会发生变化.在字符串常量中使用双 \s(像这样 \\):

\ (backslash) is an escape character within string constants, so your string ends up changed. Use double \s (like so \\) within string constants:

subprocess.Popen('rd /s /q .\\ProcessControlSimulator\\bin', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

这篇关于为什么这个 dos 命令在 python 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆