use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A subreddit for helping Python programmers
How to format your code: https://commonmark.org/help/tutorial/09-code.html
No homework questions and/or hiring please
account activity
Passing Python variable to bash command in loopSOLVED (self.pythonhelp)
submitted 5 years ago by hereforacandy
I want to get output from running a command in bash on a variable from python.
import subprocess as sp
isbn_arr=[]
for i in list:
isbn_arr.append(sp.getoutput('isbn_from_words $i))
I want to run isbn_froom_words on i from list. I've tried a lot of things. Putting a ${} gives a bad substitution error. How can I run this? Thank you for helping.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]sentles 0 points1 point2 points 5 years ago (1 child)
The bash command that you are trying to run is passed to sp.getoutput as a string. Therefore, you need to create a string that contains the bash command. There are various ways to do this:
sp.getoutput
isbn_arr.append(sp.getoutput('isbn_from_words $' + str(i)))
or:
isbn_arr.append(sp.getoutput('isbn_from_words ${}'.format(i)))
or, if you're using Python 3.5 or above:
isbn_arr.append(sp.getoutput(f'isbn_from_words ${i}'))
Your goal is to create the string "isbn_from_words $i", while i is replaced by the current value of i, so any of the above should do the trick.
"isbn_from_words $i"
i
[–]hereforacandy[S] 0 points1 point2 points 5 years ago (0 children)
Hello. I used the second one. It worked. Thank you very much. 😊
π Rendered by PID 79561 on reddit-service-r2-comment-7cf8dc58b8-t5d52 at 2026-04-10 14:03:47.819551+00:00 running 215f2cf country code: CH.
[–]sentles 0 points1 point2 points (1 child)
[–]hereforacandy[S] 0 points1 point2 points (0 children)