Python gets apk file URL address instance _python

Source: Internet
Author: User
Tags file url python script in python

It is often necessary to extract the specific URL address of the APK file in the work, so think of using Python script for automatic processing.
The Python basics you need to use are as follows:
Os.walk ()
function declaration: Os.walk (Top,topdown=true,onerror=none)
(1) The top of the parameter indicates the path of the top-level directory that needs to be traversed.
(2) The default value of the parameter topdown is "True" to first return the files in the top-level directory and then iterate through the files in the subdirectory. When the value of Topdown is "False", it means that the files in the subdirectory are traversed first, and then the files in the top-level directory are returned.
(3) The default value of parameter onerror is "None", which indicates an error in ignoring file traversal. If not NULL, provides a custom function to prompt for an error message to continue traversing or throw an exception abort traversal.
Return value: The function returns a tuple containing three elements. These three elements are: each traversal of the path name, directory list of directories, directories under the file list.
Os.walk Use instance: Delete a folder (of course it can be deleted by Os.listdir recursive calls)

Copy Code code as follows:

#! /usr/bin/env python
#coding =utf-8
Import OS

Def remove_dir (top_dir):
    if Os.path.exists (top_dir) ==false:
         print ' not EXISTS '
        return
    if Os.path.isdir (Top_dir) ==false:
        print "Not a dir"
         return
    to Dir_path,subpaths,files in Os.walk (top_dir,false):
        for file in files:
             File_path=os.path.join (dir_path,file)
             print "Delete file:%s"  %file_path
             Os.remove (file_path)
        print "Delete dir:%s"% Dir_path
        os.rmdir (dir_path)

#调用
Remove_dir (r "C:\Users\Administrator\Desktop\abc")


The way Python executes system commands Os.system (), Os.popen (), Commands.getstatusoutput ()
Os.system () cannot get to output and return values;
A file read object is returned by Os.popen (), and the operation reads read () to see the output executed, but the return value is not available.
With the Commands.getstatusoutput () method, you can get the return value and output
(status, Output) = Commands.getstatusoutput (' Cat/proc/cpuinfo ')
3. Contains (...) function of the operator module in Python
Contains (A, b)--Same as B in a (note reversed operands). Determine whether B is contained by a
Basic knowledge is finished, you can go to the code:
Copy Code code as follows:

Import OS
Import operator
Import commands
#from Signature Import *

InputDir = ". tmp"

For Path, dir, the files in Os.walk (InputDir):
For file in Files:
If not File.endswith ('. apk '):
#print "not apk file."
Continue
Apkpath = Os.path.join (inputdir, file)
cmd = './xxx-d%s '%apkpath
Output = Os.popen (cmd)
s = Set ()
#按行查找URL
For line in output:
If Operator.contains (line, "http://"):
#print tmp
Start = Line.index (' ' http://')
End = Line.index (' "" ", start)
url = line[start:end]
S.add (URL)
cmd = './yyy-t a.expense.mdk.a.tvd%s '%apkpath
#获取命令执行结果及返回值
Status, Output = Commands.getstatusoutput (cmd)
# Print Output

If Output.startswith (' Find '):
Print output

For URL in s:
If Url.find (' Imei ')!=-1:
print ' URL is%s '%url.strip ()
#print ' ========================= '
s = '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.