繁体   English   中英

找不到返回的文件或目录,但是bash命令中没有提到文件或目录?

[英]Returning file or directory not found, but no file or directory mentioned in bash command?

我正在尝试使用给出的特定命令从数据库中转储信息。

 db_dump = open('{}/dbdump.txt'.format(directory_name), 'w+')
    foo = subprocess.call('PGPASSWORD="-------" pg_dump -h vm-postgres -d "qa-test{}db" -U "qatest" -a -x'.format(str(driverlabelnum)), shell=True)
    print str(foo)
    db_dump.write(foo)
    db_dump.close()
    print "Done dumping"

当我将其复制粘贴到命令行时,它工作得很好,但是当我尝试通过subprocess.call()将其自动化时,它会出错。

我尝试了几种不同的方法,但是我一直遇到这个错误...

    Traceback (most recent call last):
  File "/home/csd-user/test/libs/utils/butler.py", line 427, in <module>
    main()
  File "/home/csd-user/test/libs/utils/butler.py", line 424, in main
    b.log_bug_exec(url)
  File "/home/csd-user/test/libs/utils/butler.py", line 52, in log_bug_exec
    cls.process_file(stdout)
  File "/home/csd-user/test/libs/utils/butler.py", line 109, in process_file
    cls.create_artifacts(nums[0], nums[1])
  File "/home/csd-user/test/libs/utils/butler.py", line 269, in create_artifacts
    foo = csd_shell_lib.check_output('PGPASSWORD="-------" pg_dump -h vm-postgres -d "qa-test{}db" -U "qatest" -a -x'.format(str(driverlabelnum)))
  File "/home/csd-user/test/libs/jupiter/csd_shell_lib.py", line 70, in check_output
    return subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT)
  File "/opt/cs/lib/python2.7/subprocess.py", line 566, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "/opt/cs/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/opt/cs/lib/python2.7/subprocess.py", line 1335, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

我知道subprocess.call命令会将命令拆分为一个substrings(.split(' '))数组/列表,并且它本身正在调用PGPASSWORD =“”。 但是,我不知道如何避免这种情况,并且无法在线找到它。

PS我遇到的另一个问题是Paramiko试图获取文件时拒绝了Errno 13权限。 我不知道/不知道如何在没有程序提示我输入密码的情况下以susudo身份登录(仅使用单数paramiko命令即可)(我可以使用ssh进行此操作,但我宁愿不这样做)。

PGPASSWORD是一个环境变量。 当必须提供特定的环境变量时,不能使用subprocess.call。 您可以改用subprocess.Popen并将环境变量作为专用参数传递。

import os, subprocess
env = os.environ.copy()
env['PGPASSWORD'] = '------'
subprocess.Popen('pg_dump -h vm-postgres -d "qa-test{}db" -U "qatest" -a -x'.format(str(driverlabelnum)), env=env, shell=True)

追溯表明您的特定错误是由于cmd.split()中的csd_shell_lib.check_output() :您不应在空格上分割任意的shell命令。 尽管什至shlex.split()在这种情况下也不起作用,因为第一项设置了一个envvar。

直接将subprocess模块与以字符串形式传递的shell命令一起使用:

cmd = 'PGPASSWORD="-------" pg_dump -h vm-postgres -d "qa-test{}db" -U qatest -a -x'.format(driverlabelnum)
output = subprocess.check_output(cmd, shell=True)

或者(更好)手动拆分命令并删除shell=True

#!/usr/bin/env python
import os
import subprocess

with open(os.path.join(directory_name, 'dbdump.txt'), 'wb', 0) as db_dump:
    subprocess.check_call(['pg_dump', '-h', 'vm-postgres',
                           '-d', 'qa-test{}db'.format(driverlabelnum),
                           '-U', 'qatest', '-a', '-x'],
                           env=dict(os.environ, PGPASSWORD="-------"),
                           stdout=db_dump)
print("Done dumping")

注意:

  • 如果pg_dump失败,则使用check_call()引发异常
  • stdout=db_dump用于将pg_dump的输出直接重定向到文件,而无需将其加载到内存中
  • 没有shell=True
  • -d-U值周围没有引号
  • 没有不必要的str()调用
  • dict(os.environ, PGPASSWORD="-------")用于为子进程创建环境dict

如果需要,请添加stderr=subprocess.STDOUT以将stderr重定向到stdout(因此将其与其余数据一起转储到文件中)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM