在硬盘上使用文件而不是使用python的url

时间:2017-02-23 15:19:59

标签: python python-2.7 python-3.x

我想修改此脚本以使用脱机文件,如果我从url工作下载文件,但如果我从硬盘驱动器退出的文件相同,则无法打开,有人帮我理解为什么以及如何做谢谢你。

    def INDEX():
    TVLIST('https://www.*********/playlist/*******/test.m3u')


def TVLIST(url):
    try:
        m3u = getHtml(url)
        parsem3u(m3u)
    except:
        addDir('Nothing found', '', '', '', Folder=False)
    xbmcplugin.endOfDirectory(int(sys.argv[1]))

urlopen = urllib2.urlopen
Request = urllib2.Request

def getHtml(url, referer=None, hdr=None, data=None):
    if not hdr:
        req = Request(url, data, headers)
    else:
        req = Request(url, data, hdr)
    if referer:
        req.add_header('Referer', referer)
    if data:
        req.add_header('Content-Length', len(data))
    response = urlopen(req)
    if response.info().get('Content-Encoding') == 'gzip':
    buf = StringIO( response.read())
    f = gzip.GzipFile(fileobj=buf)
    data = f.read()
    f.close()
else:
    data = response.read()    
response.close()
return data

def parsem3u(html, sitechk=True):
    match = re.compile('#.+,(.+?)\n(.+?)\n').findall(html)
    txtfilter = txtfilter = GETFILTER()
    txtfilter = txtfilter.split(',') if txtfilter else []
    txtfilter = [f.lower().strip() for f in txtfilter]
    i = 0
    count = 0
    for name, url in match:
        status = ""
        url = url.replace('\r','')
        if not txtfilter or any(f in name.lower() for f in txtfilter):
            if sitechk:
                if i < 5:
                    try:
                        siteup = urllib.urlopen(url).getcode()
                        status = " [COLOR red]offline[/COLOR]" if siteup != 200 else " [COLOR green]online[/COLOR]"
                    except: status = " [COLOR red]offline[/COLOR]"
                    i += 1
            addPlayLink(name+status, url, 3, uiptvicon)
            count += 1
    return count

我认为,足以放置本地路径

def INDEX():
TVLIST(r'c:\Desktop\IPTVLIST\M3U\playlist\test.m3u')

谁解释了为什么它不起作用,我该怎么办?谢谢

1 个答案:

答案 0 :(得分:0)

正如@languitar在评论中所建议的那样file://当然它应该适用于Windows,但是转移到像android这样的平台,你有不同的文件系统,你没有C开车。因此,请确保您在Android上有另一个位置。