2

1) システムコマンド (すなわち "dir" ) を実行する python プログラムを書く 2) システムコマンドの出力を変数に保存する 3) 変数を出力する

これは Python で行われます。私はこれを理解できません。サブプロセスを使用して「0」のみを返すものを見つけました。

Windows 7 Python 2.5 および 2.7 を使用しています

基本的に、 cmd -> dir C:\ のような出力が必要です

次に、その出力が Python を使用してファイルに保存されます。

助けてくれればいいのに、

4

2 に答える 2

2

使用できますsubprocess.check_output

ドキュメントから:

subprocess.check_output(args, stdin=None, stderr=None, shell=False, universal_newlines=False)

引数を指定してコマンドを実行し、その出力をバイト文字列として返します。

例:

>>> subprocess.check_output(["echo", "Hello World!"])
'Hello World!\n'
于 2012-12-12T20:35:49.897 に答える
0

これは、あなたが探しているものを達成するように見える他の場所で見つけたクラスです

class Command(object):
    """Run a command and capture it's output string, error string and exit status"""
    def __init__(self, command):
        self.command = command
    def run(self, shell=True):
        import subprocess as sp
        process = sp.Popen(self.command, shell = shell, stdout = sp.PIPE, stderr = sp.PIPE)
        self.pid = process.pid
        self.output, self.error = process.communicate()
        self.failed = process.returncode
        return self
    @property
    def returncode(self):
        return self.failed

実行するには、次のようにします。

commandVar = Command("dir").run()

次に、結果を表示します。

commandVar.output
于 2012-12-12T20:44:04.363 に答える