rad*_*rad 3 python subprocess python-2.6
我目前正在为客户编写脚本。
该脚本从配置文件读取。然后将其中一些信息存储在变量中。
之后,我想使用subprocess.call执行安装命令,所以我正在使用这些变量来构建安装命令
call("mount -t cifs //%s/%s %s -o username=%s" % (shareServer, cifsShare, mountPoint, shareUser))
Run Code Online (Sandbox Code Playgroud)
但是这不起作用
Traceback (most recent call last):
File "mount_execute.py", line 50, in <module>
main()
File "mount_execute.py", line 47, in main
call("mount -t cifs //%s/%s %s -o username=%s" % (shareServer, cifsShare, mountPoint, shareUser))
File "/usr/lib64/python2.6/subprocess.py", line 470, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib64/python2.6/subprocess.py", line 623, in __init__
errread, errwrite)
File "/usr/lib64/python2.6/subprocess.py", line 1141, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Run Code Online (Sandbox Code Playgroud)
首先使用以下命令
mountCommand = 'mount -t cifs //%s/%s %s -o username=%s' % (shareServer, cifsShare, mountPoint, shareUser)
call(mountCommand)
Run Code Online (Sandbox Code Playgroud)
也会导致相同的错误。
您当前的调用是为与一起使用而编写的shell=True,但实际上并未使用它。如果您确实要使用需要用shell解析的字符串,请使用call(yourCommandString, shell=True)。
更好的方法是传递一个明确的参数列表-使用来shell=True使命令行解析依赖于数据的详细信息,而传递一个明确的列表意味着您自己做出解析决策(作为一个您了解的人,您正在运行的命令更适合执行此操作)。
call(['mount',
'-t', 'cifs',
'//%s/%s' % (shareServer, cifsShare),
mountPoint,
'-o', 'username=%s' % shareUser])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1953 次 |
| 最近记录: |