相关疑难解决方法(0)

subprocess.call使用字符串vs使用列表

我试图使用rsync与subprocess.call.奇怪的是,如果我传递subprocess.call一个字符串,它可以工作,但它不适用于列表(ala,Python的doc).

使用字符串调用sp.call:

In [23]: sp.call("rsync -av content/ writings_raw/", shell=True)
sending incremental file list

sent 6236 bytes  received 22 bytes  12516.00 bytes/sec
total size is 324710  speedup is 51.89
Out[23]: 0
Run Code Online (Sandbox Code Playgroud)

使用列表调用sp.call:

In [24]: sp.call(["rsync", "-av", "content/", "writings_raw/"], shell=True)
rsync  version 3.0.9  protocol version 30
Copyright (C) 1996-2011 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
    64-bit files, 64-bit inums, 32-bit timestamps, 64-bit long ints,
    socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
    append, ACLs, xattrs, iconv, symtimes

rsync comes …
Run Code Online (Sandbox Code Playgroud)

python subprocess

27
推荐指数
1
解决办法
2万
查看次数

Python - 用很多参数调用popen

我试图用一个参数列表调用popen.

    execString = "java -jar {} {} {} {} {} {}".format(os.path.join(config.java_root,
                                                                   config.java_jar),
                                               self.canvasSize,
                                               self.flightId,
                                               self.domain,
                                               self.defPath,
                                               self.harPath)
    execStringList = execString.split()
    print execStringList
    subprocess.Popen([execStringList])
Run Code Online (Sandbox Code Playgroud)

execStringList是:

['java', '-jar', '/Users/me/Projects/reporting-test/build/clickunit-0.1.jar', '300x1050', '123', 'dev.me.net', '/Users/me/Projects/reporting-test/src/definitions/300x1050.yml', '/Users/me/Projects/reporting-test/out/01/15112']
Run Code Online (Sandbox Code Playgroud)

根据:Python OSError:[Errno 2]是正确的格式.但是,我收到以下错误:

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 672, in __init__
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1202, in _execute_child
AttributeError: 'list' object has no attribute 'rfind'
Run Code Online (Sandbox Code Playgroud)

如果我将execString视为字符串,我会得到一个不同的错误:

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 672, in __init__
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1202, in _execute_child
OSError: [Errno 2] No such file or directory
Run Code Online (Sandbox Code Playgroud)

即使我从终端运行此命令,它仍然有效.

$> java …
Run Code Online (Sandbox Code Playgroud)

python exec popen

1
推荐指数
1
解决办法
2346
查看次数

标签 统计

python ×2

exec ×1

popen ×1

subprocess ×1