0

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!

BruceWayne
  • 22,923
  • 15
  • 65
  • 110
  • @alec_a - See my edit. I can't do something like `for fname in os.listdir('/storage/emulated/0/Pictures'):` as far as I know, because that directory exists via SSH connection, not a physical (i.e. USB) connection. – BruceWayne Apr 25 '19 at 04:40
  • Then why are you not asking about that? Don't use `find` if that's not what you want. – tripleee Apr 25 '19 at 05:04
  • Just for your info: There is no such thing as a "Linux command". `find` is a program, other such things like `jobs` and `bg` are rather shell builtins (i.e. a feature of the programming language that the shell provides). – Ulrich Eckhardt Apr 25 '19 at 05:18
  • @UlrichEckhardt - Thanks for that clarification. I am pretty unfamiliar with Linux, and didn't know what else to call that. – BruceWayne Apr 25 '19 at 05:22
  • @tripleee - I do want the find command (program) though - I just want to keep the found results to be in the directory I pass to it, not sub-directories of it too. Also, unless I'm missing something (which is certainly possible), the answer in that question marked as duplicate also uses `find`, no? `...client.exec_command( 'find ...`? – BruceWayne Apr 25 '19 at 05:24
  • 1
    True. The option you are looking for to not descend into subdirectories is `-maxdepth 1` – tripleee Apr 25 '19 at 05:52
  • @tripleee that seemed to do it, thanks! – BruceWayne Apr 25 '19 at 22:44

0 Answers0