0

サブプロセス モジュールを使用して、Python でコマンドを実行しています。しかし、問題は、コマンドに (ファイル名の) 文字列も含めたいということです。

私がやりたいことの例:

  from subprocess import call

  command = "cd/DirectoryName"

  call = [(command)]

この特定の例では、DirectoryName をユーザーが決定する変数にします。

私が無駄にしようとしたこと:

  Desktop=raw_input()
  cmd="'cd %s'(Desktop/)"
  call([cmd])

Python シェルでこれらのコマンドを実行しようとすると、次のエラーが表示されます。

    Chicken='Chicken'
    command = 'say %s' % (Chicken)
    print command
    say Chicken
    call([command])

    Traceback (most recent call last):
    File "/Applications/WingIDE.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 1,     in <module>
    # Used internally for debug sandbox under external interpreter
    File "/Library/Frameworks/EPD64.framework/Versions/7.2/lib/python2.7/subprocess.py", line 493, in call
    return Popen(*popenargs, **kwargs).wait()
    File "/Library/Frameworks/EPD64.framework/Versions/7.2/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
    File "/Library/Frameworks/EPD64.framework/Versions/7.2/lib/python2.7/subprocess.py", line 1228, in _execute_child
    raise child_exception
    OSError: [Errno 2] No such file or directory

これを試しただけで、シェルがクラッシュしました。

    Chicken="Chicken"
    print Chicken
    Chicken
    call[("say %s" % (Chicken)]
4

2 に答える 2

1

それは文字列補間の仕組みではありません。

cmd='cd %s' % (Desktop,)
于 2012-04-12T16:59:02.817 に答える