简体   繁体   中英

working with python xml parser

import sys
import os
import subprocess
from xml.dom import minidom

user_input = raw_input("Enter the path of your file: ")
#f = open(user_input,'r+')
#str(f)
apktool = "/usr/share/apktool/apktool.jar"
os.system(apktool + " d " +  user_input)
base = os.path.basename(user_input)
a = os.path.splitext(base)[0]
#b = os.path.dirname(user_input)
d = os.path.abspath(os.path.splitext(base)[0])
#print d
#print a
for root,dirs,files in os.walk(os.path.abspath(os.path.splitext(base)[0])):
    for filename in files:
        if filename.startswith('AndroidManifest'):
            pth = os.path.realpath(os.path.join(root,filename))
            print pth

Enter the path of your file: /roo/Desktop/test.apk

After I run the script the 'pth' output gives the following paths: /root/Desktop/test/AndroidManifest.xml and /root/Desktop/test/org/AndroidManifest.xml

However my desired output for 'pth' is /root/Desktop/test/AndroidManifest.xml How do I achieve this?

The value of root changes as it goes through the folders.
At some point it becomes /root/Desktop/test/org/ which is how you get
/root/Desktop/test/org/AndroidManifest.xml as a path when you do the
pth = os.path.realpath(os.path.join(root,filename))

It seems like the path you want is only the very first value of root , in which case you can try:
pth = os.path.join(os.path.abspath(os.path.splitext(base)[0]), filename)
Which is basically joining the path that you are passing to os.walk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM