简体   繁体   English

使用 Python 使用 ADB 列出文件

[英]listing files with ADB using Python

I have a list of files under a directory in my Android device which I want to list, however I only want files without a dot(that is - no hidden files and files with explicit extensions).我想列出我的 Android 设备的目录下的文件列表,但是我只想要没有点的文件(即 - 没有隐藏文件和具有显式扩展名的文件)。

I can do that in the mobile device with find :我可以在移动设备上使用find做到这一点:

find /mydir -type f -not -name *.*

And I can do the same with adb from my host (here I need the \" to protect my regex)我可以对主机上的adb做同样的事情(这里我需要\"来保护我的正则表达式)

adb shell find /mydir -type f -not -name \"*.*\"

However I don't have a way to get it working in Python script triggered from my host.但是我没有办法让它在我的主机触发的 Python 脚本中工作。 What I did is:我所做的是:

list_command = [
            'adb',
            'shell',
            'find',
            '/mydir'
            '-type',
            'f',
            '-not',
            '-name',
            r'\'*\.*\''
        ]
subprocess.check_output(list_command)
            .decode()
            .splitlines()

It actually gave me all the files regardless of whether a dot is presented in the path.无论路径中是否出现点,它实际上都给了我所有文件。 What is the right way to run this adb command in Python?在 Python 中运行此adb命令的正确方法是什么?

Since you are still passing a glob, not a regex, you should not escape the dot characters, use "'*.*'" :由于您仍在传递 glob,而不是正则表达式,因此不应转义点字符,请使用"'*.*'"

list_command = [
            'adb',
            'shell',
            'find',
            '/mydir'
            '-type',
            'f',
            '-not',
            '-name',
            "'*.*'"
        ]

Just use it inside single quotes.只需在单引号内使用它。 Note you do not need to escape single quotes in a double quoted string literal, "'*.*'" is cleaner than the corresponding '\'*.*\'' single quoted string literal.请注意,您不需要在双引号字符串文字中转义单引号, "'*.*'"比相应'\'*.*\''单引号字符串文字更干净。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM