3

I am trying to find the path to a file named 'config.txt' from a usb flash drive plugged into a raspberry pi. The physical drive that is used may not always be the same, so the path may not always be the same. So I use

'find /media/pi/*/config.txt' 

to locate the path in the terminal and works just fine. Now I go to use check_output and get a giant string of paths.

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

I set the shell as True to allow wild cards, according to https://docs.python.org/2/library/subprocess.html

Results for out are:

'.\n./.Xauthority\n./.xsession-errors\n./Public\n./.dmrc\n./Downloads\n./test.sh\n./.idlerc\n./.idlerc/recent-files.lst\n./.idlerc/breakpoints.lst\n./.asoundrc\n./.bash_logout\n./.profile\n./Templates\n./Music\n./.bash_history\n./Videos\n./.local\n./.local/share\n./.local/share/gvfs-metadata\n./.local/share/gvfs-metadata/home\n./.local/share/gvfs-metadata/home-d6050e94.log\n./.local/share/applications\n./.local/share/recently-used.xbel\n./.local/share/Trash\n.....

And it continues to go on for awhile. I tried looking at several other similar questions including the link below, but no luck.

Store output of subprocess.Popen call in a string

Community
  • 1
  • 1
Matt
  • 115
  • 12
  • Your command line shows `Find` with a capital F. Is that the name of the executable? On most or all OS's you need to get the case correct. – Chris Johnson Dec 11 '15 at 15:51
  • Consider using the [`glob`](https://docs.python.org/3.1/library/glob.html) module to find file paths, rather than invoking system commands. – Kevin Dec 11 '15 at 15:53
  • @ChrisJohnson, I mistyped that. f is lowercase when used in terminal. – Matt Dec 11 '15 at 15:57

2 Answers2

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')
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • I was not aware of glob, thank you. I find this answer most useful due to the lack of newline characters in the output and if for some reason there are multiple results, each result is separated into its own item in a list. – Matt Dec 11 '15 at 16:05
  • @Matt, no worries, yes glob returns a list of matches, you could also use `next(iglob('/media/pi/*/config.txt'))` if you will always have just the one or simply index the list returned – Padraic Cunningham Dec 11 '15 at 16:07
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.

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159