使用python xml PAR

2024-06-16 11:42:07 发布

您现在位置:Python中文网/ 问答频道 /正文

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

运行脚本后,“pth”输出提供以下路径: /root/Desktop/test/AndroidManifest.xml/root/Desktop/test/org/AndroidManifest.xml

然而,我对“pth”的期望输出是/root/Desktop/test/AndroidManifest.xml 我如何做到这一点


Tags: pathtestimportinputbaseosrootxml
1条回答
网友
1楼 · 发布于 2024-06-16 11:42:07

root的值在遍历文件夹时会发生变化。
在某种程度上,它变成了/root/Desktop/test/org/,这就是你得到的
/root/Desktop/test/org/AndroidManifest.xml作为执行
pth = os.path.realpath(os.path.join(root,filename))

看起来您想要的路径只是root的第一个值,在这种情况下,您可以尝试:
pth = os.path.join(os.path.abspath(os.path.splitext(base)[0]), filename)
基本上就是连接你要传递给os.walk的路径

相关问题 更多 >