首页 > python > 有人可以解释Python的子进程模块如何与命令提示符通信?

有人可以解释Python的子进程模块如何与命令提示符通信? (Can someone explain how Python's subprocess module communicates with Command Prompt?)

问题

我是编程的新手,并且一直在倾倒这个网站以及其他人以更好地理解我如何编写Python(版本3.4.1)中的脚本,该脚本执行我已经知道如何在命令提示符(版本6.3.9600)中执行的操作。

在命令提示符中,我可以很容易地输入(在“>”之后):

Z:\input\convertToRinex.exe Z:\input\74390010.T01

一切都很完美。转换.T01文件,生成的文件出现在输入文件夹中。

在Python中我不知道我在做什么。我试过这个:

from subprocess import *
call(['Z:\input\convertToRinex.exe', 'Z:\input\74390010.T01'])

我得到了这个错误

Error: CtrimbleFile - unable to open file:Z:\psm_rinex\inputU90010.T01Open

在一个窗口中,顶部显示Z:\ input \ convertToRinex.exe。它发生得太快了,我不得不使用屏幕录像机来读取错误信息。

我认为Python中的列表将被翻译成已经在命令提示符中使用的完全相同的语法,同样的事情会发生。

我显然遗漏了一些东西。任何建议将不胜感激。

解决方法

反斜线里面有在Python中(和几乎所有其他编程语言)有特殊的含义记录在这里,所以你需要编写任何

call(['Z:\\input\\convertToRinex.exe', 'Z:\\input\\74390010.T01'])

要么

call([r'Z:\input\convertToRinex.exe', r'Z:\input\74390010.T01'])

问题

I am very new to programming and have been pouring over this site and others to better understand how I can write a script in Python (version 3.4.1) that does what I already know how to do in Command Prompt (version 6.3.9600).

In Command Prompt I can very easily type this (after the ">"):

Z:\input\convertToRinex.exe Z:\input\74390010.T01

And everything works perfectly. The .T01 file is converted and the resulting files appear in the input folder.

In Python I have no idea what I am doing. I've tried this:

from subprocess import *
call(['Z:\input\convertToRinex.exe', 'Z:\input\74390010.T01'])

And I get this error

Error: CtrimbleFile - unable to open file:Z:\psm_rinex\inputU90010.T01Open

In a window that says Z:\input\convertToRinex.exe at the top. It happens so fast that I had to use a screen recorder to read the error message.

I thought the list from Python would be translated into the exact same syntax that already works in Command Prompt and the same thing would happen.

I am clearly missing something. Any advice would be greatly appreciated.

解决方法

Backslashes have a special meaning inside strings in Python (and almost every other programming language) as documented here, and so you need to write either

call(['Z:\\input\\convertToRinex.exe', 'Z:\\input\\74390010.T01'])

or

call([r'Z:\input\convertToRinex.exe', r'Z:\input\74390010.T01'])
相似信息