Search code examples
androidpythonstringadb

Python - String appending Issue


I'm working on a python script which takes the output from a previous terminal window command and inputs again. Here is the code

 pathCmd = './adb shell pm path com.example.deliveryupdater'
 pathData = os.popen(pathCmd,"r")
 for line in pathData:
  path = line
  print line   

if line.startswith("package:"):
   apkPath = line[8:] 
   print apkPath
pullCmd = './adb pull ' + apkPath
pullData = os.popen(pullCmd,"r")

The output is as follows: /data/app/com.example.deliveryupdater-1.apk

' does not exist/data/app/com.example.deliveryupdater-1.apk

It says the path doesn't exist. When I hardcoded the path as

 pullCmd = './adb pull /data/app/com.example.deliveryupdater-1.apk'
 pullData = os.popen(pullCmd,"r")

The .apk data gets pulled.

3886 KB/s (2565508 bytes in 0.644s)

Is there a way I can pass as the string as a variable? Am I doing anything wrong here? Please help


Solution

  • The error message is telling you what's wrong: that path, /data/app/com.example.deliveryupdater-1.apk(newline), does not exist. Probably there is not a filename ending with a newline in the directory. I assume you are iterating over lines from a file or something of that sort, which would explain why you have the newline. Why not just slice [8:-1] instead of [8:], or perhaps, just .rstrip() on the line (this will work even if the line doesn't have a newline, as the last line in the file might not)?

    if line.startswith("package:"):
       apkPath = line[8:].rstrip()
       print apkPath
    pullCmd = './adb pull ' + apkPath
    pullData = os.popen(pullCmd,"r")