1. 程式人生 > 實用技巧 >python學習雜記--讀取安卓裝置sdcard中的檔案列表

python學習雜記--讀取安卓裝置sdcard中的檔案列表

在讀取安卓手機sdcard中的檔案列表時

有幾點需要注意的

1.首先進入手機的sdcard檔案列表(為了便於操作,生成了一個名為streamlist.txt的檔案來存放檔名)

1 file_path = "sdcard/itemfile"
2 file_stream_path = []
3 os.system("adb shell ls %s >>streamlist.txt 2>&1" % file_path)
4 with open("streamlist.txt", 'r+') as sl:
5     for line in sl:
6         file_stream_path.append(
7 str("/" + file_path + "/" + line).replace("\n", ""))

這裡注意的是,目標檔案的表達方式是“sdcard/itemfile”,而不是“/sdcard/itemfile”

2.如果要通過cmd在指定的檔案中寫入輸出資訊,有兩種方法os.system和os.popen

os.system的形式如下

1 os.system("adb shell input keyevent %s >>output.txt 2>&1"%i)
2 或者
3 os.system("echo -------------------------- >>output.txt 2>&1
")

os.system的結果不能輸出到控制檯,輸出的只能是指令的執行結果(0,1,2,0表示成功)只能 執行 or 寫入其他檔案中

os.popen的形式如下