質問

I'm trying to run a script and return the stdout (output).

The code calling the script is:

def read_wl_file(self, wl_file, script):
    p = subprocess.Popen([script, wl_file], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    return p.stdout.read()

And the script is below with the one and only arg being the file to process

#!/bin/bash

usage(){
    echo "Usage: $0 processes the thingy file."
    exit 1 # error
}

# no arg, error
is_file_exits(){
    local f="$1"
    [[ -f "$f" ]] && return 0 || return 1
}

# invoke  usage if filename not supplied
[[ $# -eq 0 ]] && usage

# Invoke is_file_exits
if ( is_file_exits "$1" )
then
    #parse file code here....

else
    echo "File not found"
fi

I can run this from my shell with no issues, but not in a python script, as my unit test fails.

def test_can_decypt(self):
    output = self.data.read_wl_file('/Users/Bryan/Desktop/data.csv', "./xxx/script.sh")
    print output
    assert "Usage: ./xxx/script.sh processes thingy file." not in output

result:

Usage: ./xxx/script.sh processes thingy file.

 F.
    ======================================================================
    FAIL: test_can_work (tests.file_tests.TestProcess)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File ".....test.py", line 17, in test_can_work
        assert "Usage: ./xxx/script.sh processes the thingy file." not in output
    AssertionError

----------------------------------------------------------------------
Ran 2 tests in 0.006s

so the test is telling me that when Popen runs it's not passing in the arg, so the usage is the output which is in error (hence the failed test). I'm lost at this point. The code sees the script, opens it but passes no arg?

役に立ちましたか?

解決

generally, you should either pass a list, or pass a single string with shell=True. Not both.

In your case, it doesn't look like you're using the shell, so I would just remove that keyword:

p = subprocess.Popen([script, wl_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top