Search code examples
pythoncmdadbenvironment

How can I get the info in cmd when I set ADB_TRACE=adb


I tried to get the progress when pushing a file to device. It works when I "set ADB_TRACE=adb" in cmd (found in this page)

Then I want to use it in python 2.7.

cmd = "adb push file /mnt/sdcard/file"
os.putenv('ADB_TRACE', 'adb')
os.popen(cmd)
print cmd.read()

It shows nothing. How can I get these details?

OS:win7


Solution

  • os.popen is deprecated:

    Deprecated since version 2.6: This function is obsolete. Use the subprocess module. Check especially the Replacing Older Functions with the subprocess Module section.

    Use subprocess instead:

    import subprocess as sp
    
    cmd = ["adb","push","file","/mnt/sdcard/file"]
    mysp = sp.popen(cmd, env={'ADB_TRACE':'adb'}, stdout=sp.PIPE, stderr=sp.PIPE)
    stdout,stderr = mysp.communicate()
    
    if mysp.returncode != 0:
        print stderr
    else:
        print stdout