简体   繁体   English

使用adb shell获取Android目录中的文件数

[英]Get number of files within Android directory using adb shell

I am currently trying to use the following python code to get the number of files within a directory. 我目前正在尝试使用以下python代码获取目录中文件的数量。

    output = subprocess.check_output(["adb", "shell", "cd /sdcard/Dir/Subdir", "find . -type f | wc -l"])
    print output

However, I am getting the output: 但是,我得到的输出:

/system/bin/sh: cd: too many arguments

0

But If I enter the following in command prompt, I get the desired result (14 files): 但是,如果我在命令提示符下输入以下内容,则会得到所需的结果(14个文件):

adb shell
cd /sdcard/Dir/Subdir
find . -type f | wc -l

Any idea how to integrate the command line code into python? 任何想法如何将命令行代码集成到python中?

if you pass arguments to check_output in a list (not in a string), then if you pass arguments with spaces they will be quoted. 如果在列表中(而不是在字符串中)将参数传递给check_output ,则如果将参数传递给空格,则将其引起引用。

But that's not the main problem here. 但这不是这里的主要问题。

You're running adb shell in check_output , but next commands should be passed through proper command line or standard input of your shell. 您正在check_output运行adb shell ,但是下check_output命令应该通过正确的命令行或shell的标准输入传递。

You could try the following (untested): 您可以尝试以下(未试用):

output = subprocess.check_output(["adb", "shell", "cd /sdcard/Dir/Subdir", "&&","find . -type f | wc -l"])

( && tells the shell to chain cd with find instead of passing all arguments to cd ) &&告诉外壳程序将cdfind而不是将所有参数都传递给cd

or run your shell interactively, and pass the commands like if you typed them (use Popen for that): 或以交互方式运行您的shell,并像输入命令一样传递命令(为此使用Popen ):

p = subprocess.Popen(["adb", "shell"],stdin=subprocess.PIPE)
out,err = p.communicate("cd /sdcard/Dir/Subdir\nfind . -type f | wc -l\n")

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

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