[英]Android adb shell - Find and replace text within a file using command
[英]Get number of files within Android directory using adb shell
我目前正在尝试使用以下python代码获取目录中文件的数量。
output = subprocess.check_output(["adb", "shell", "cd /sdcard/Dir/Subdir", "find . -type f | wc -l"])
print output
但是,我得到的输出:
/system/bin/sh: cd: too many arguments
0
但是,如果我在命令提示符下输入以下内容,则会得到所需的结果(14个文件):
adb shell
cd /sdcard/Dir/Subdir
find . -type f | wc -l
任何想法如何将命令行代码集成到python中?
如果在列表中(而不是在字符串中)将参数传递给check_output
,则如果将参数传递给空格,则将其引起引用。
但这不是这里的主要问题。
您正在check_output
运行adb shell
,但是下check_output
命令应该通过正确的命令行或shell的标准输入传递。
您可以尝试以下(未试用):
output = subprocess.check_output(["adb", "shell", "cd /sdcard/Dir/Subdir", "&&","find . -type f | wc -l"])
( &&
告诉外壳程序将cd
与find
而不是将所有参数都传递给cd
)
或以交互方式运行您的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.