python adb 获取文件修改时间

import time

import subprocess
import time

def call_adb(adb_str):
    output = subprocess.Popen(adb_str, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0]
    # print(f'{time.strftime("%m%d:%H%M")} adb exec' , output.decode('utf-8').replace("\n",""))
    return output.decode('utf-8').replace("\n","")
from datetime import datetime
if __name__ == '__main__':
    test_ip = 'xxxx'
    adb_str=rf'adb -s {test_ip} shell stat /sdcard/aaa.txt'
    res=call_adb(adb_str)

    s1=res.split('\r')[6]
    s1 = s1.split()[1] + " " + s1.split()[2].split('.')[0] + '.' + s1.split()[2].split('.')[1][:6] + " " + s1.split()[3]
    timestamp1 = datetime.strptime(s1, "%Y-%m-%d %H:%M:%S.%f %z").timestamp()
    s2 = "Change: 2023-07-15 16:02:30.746997521 +0800"

    s2 = s2.split()[1] + " " + s2.split()[2].split('.')[0] + '.' + s2.split()[2].split('.')[1][:6] + " " + s2.split()[3]

    timestamp2 = datetime.strptime(s2, "%Y-%m-%d %H:%M:%S.%f %z").timestamp()

    # 计算时间差
    time_diff = time.time() - timestamp1

    # 打印时间差(单位:秒)
    print("Time difference:", time_diff, "seconds")

猜你喜欢

转载自blog.csdn.net/jacke121/article/details/131741456