The Python Oracle

python check output find command not working

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: A Thousand Exotic Places Looping v001

--

Chapters
00:00 Question
01:15 Accepted answer (Score 1)
01:38 Answer 2 (Score 0)
02:02 Thank you

--

Full question
https://stackoverflow.com/questions/3422...

Question links:
https://docs.python.org/2/library/subpro...
[Store output of subprocess.Popen call in a string]: https://stackoverflow.com/questions/2502...

Accepted answer links:
[glob]: https://docs.python.org/2/library/glob.h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #find #subprocess #raspberrypi2

#avk47



ACCEPTED ANSWER

Score 1


You would need to pass a single string exactly as you would from your shell if you want to use a wildcard:

from subprocess import check_output
cmd = 'find /media/pi/*/config.txt'
out = check_output(cmd,shell=True)

You don't actually need subprocess at all, glob will do what you want:

from glob import glob 
files =  glob('/media/pi/*/config.txt')



ANSWER 2

Score 0


Since you are using the shell=True you can use the following:

from subprocess import check_output
cmd = 'cd /media/pi && find . -iname config.txt'
out = check_output(cmd, shell=True) 

Try to avoid use of wildcards if possible, and just change your current working directory before searching for your target file.