简体   繁体   English

Python无法识别大于号吗?

[英]Python not recognizing the greater than sign?

I am using Python to create windows commands using subprocess.call(command) where command is a string I've generated for the Windows command. 我正在使用Python通过subprocess.call(command)创建Windows命令,其中command是我为Windows命令生成的字符串。 I need the results of my command to output to a .txt file so I use 2>> C:\\Users\\me\\out.txt as part of command except Python does not seem to recognize the greater than character, > . 我需要命令的结果输出到.txt文件,因此我将2>> C:\\Users\\me\\out.txt用作command一部分,但Python似乎无法识别大于字符> I've tried using the Unicode value, u'\>' too. 我也尝试过使用Unicode值u'\>'

[EDIT] If I copy command and paste it into my command prompt, then it will execute the command properly. [编辑]如果我复制command并将其粘贴到命令提示符下,则它将正确执行命令。 Otherwise it won't work from my Python script. 否则,它将无法在我的Python脚本中运行。

Python has nothing to do with that. Python与之无关。

If you do 如果你这样做

subprocess.call("command 2>>out.txt", shell=True)

it is the shell which does this part of redirection. 这是外壳,负责重定向的这一部分。

If you don't work with the shell, it cannot work. 如果您不使用外壳,它将无法正常工作。 In this case, you better do 在这种情况下,您最好

with open("out.txt", "a") as outfile:
    subprocess.call(["command"], stderr=outfile)

如果使用的是诸如重定向之类的外壳结构,则需要参数shell=True

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

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