0

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?

1 Answer 1

2

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) 
Sign up to request clarification or add additional context in comments.

4 Comments

This was it and I can't believe that was the issue. Thank you.
In all of my grepping for the issue, I never saw a reason to NOT use the shell arg, but I'm sure that's in a doc somewhere. thanks again.
+1: a list argument and shell=True together is almost always an error.
@J.F.Sebastian -- Yep. And thanks for calling me out on the other question (now deleted)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.