在忽略子目录的目录中查找文件-通过python的Linux cmd

时间:2019-04-25 04:10:03

标签: python linux

我正在尝试通过Python获取目录中的文件列表,并通过SSH将其传输到Android手机中。我认为“ find”命令本身是基于Linux命令的,但是是通过Python发送的。

如何让命令仅搜索提供给我的目录,而不搜索子目录?

这是基本功能:

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()

其中directory例如'/storage/emulated/0/Pictures/'的“图片”文件夹中有子目录,即“背景”,“桌面”,“ GIFS”等。

我只想返回.../Pictures文件夹中的文件。我在seeing elsewhere时添加了-prune行,但这是一种方法,但它不起作用(我在/Pictures内的所有目录中都找到文件列表)。

编辑:请注意,我正在使用通过SSH连接到Android并在其中发出命令-通过linux。我不能(AFAIK?)只做os.walk或类似的命令,我必须通过linux发出命令。但是,如果我弄错了,请告诉我!

0 个答案:

没有答案
相关问题