簡體   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