I am trying to get a list of files in a directory, via Python, SSH'ed into an Android phone. The "find" command itself is I believe based on Linux commands, but sent via Python.
How can I have the command search only the directory I feed it, without searching subdirectories?
Here's the basic function:
def list_files(directory, filetype, ssh=""):
"""
This will scan a directory for the filetype,
which is passed as `.jpg`, or `.mp3`, etc. and return
a list of those files. Note that the -prune tag ignores
subdirectories.
"""
print("Collecting filenames of all photos in", directory)
distantFiles = []
if ssh != "":
filePath = directory
filePattern = '"*' + filetype + '"'
rawcommand = 'find {path} -name {pattern} -prune'
command = rawcommand.format(path=filePath, pattern=filePattern)
stdin, stdout, stderr = ssh.exec_command(command)
filelist = stdout.read().splitlines()
where directory
would be e.g. '/storage/emulated/0/Pictures/'
and in the "Pictures" folder there are subdirectories, i.e. "Background", "Desktop", "GIFS", etc.
I want to only return files in the .../Pictures
folder. I added the -prune
line as I was seeing elsewhere that would be a way to do it, but it isn't working (I'm getting a list of files in all directories within /Pictures
).
Edit: Note that I am using connecting to the Android via SSH and issuing a command there - via linux. I can't (AFAIK?) just do os.walk
or similar, I have to issue the command via linux. However, if I'm mistaken, please let me know!