如何读取所有文件并为一个def执行相同的工作?

2024-06-26 00:10:04 发布

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

我用的是def

m3ufile= 'IPTV1'
M3UPATH= '/tmp/IPTV/IPTV1.m3u'
BFNAME= 'userbouquet.IPTV1.tv'
LastScanned= 'userbouquet.LastScanned.tv'
WGET='wget --no-check-certificate'
BOUQUETSPATH='/etc/enigma2/bouquets.tv'
E2SETTINGSPATH='/etc/enigma2'
##################################################################

def convert():
       with open('%s/%s' % (E2SETTINGSPATH, BFNAME), 'w') as outfile:
        desk_tmp = ''
        outfile.write('#NAME %s\r\n' % m3ufile)
        if os.path.isfile(M3UPATH):
          for line in open(M3UPATH):
            if line.startswith('http://'):
                    outfile.write('#SERVICE 4097:0:1:0:0:0:0:0:0:0:%s' % line.replace(':', '%3a'))
                    outfile.write('#DESCRIPTION %s' % desk_tmp)
            elif line.startswith('#EXTINF'):
                    desk_tmp = '%s' % line.split(',list.alliptvlinks.com ')[-1]
            elif '<stream_url><![CDATA' in line:
                    outfile.write('#SERVICE 4097:0:1:0:0:0:0:0:0:0:%s\r\n' % line.split('[')[-1].split(']')[0].replace(':', '%3a'))
                    outfile.write('#DESCRIPTION %s\r\n' % desk_tmp)
            elif '<title>' in line:
                    if '<![CDATA[' in line:
                        desk_tmp = '%s\r\n' % line.split('[')[-1].split(']')[0]
                    else:
                        desk_tmp = '%s\r\n' % line.split('<')[1].split('>')[1]
          outfile.write('\r\n')           
          outfile.close()
          if os.path.isfile(BOUQUETSPATH):
           for line in open(BOUQUETSPATH):
            if BFNAME in line:
                  if os.path.isfile('%s/%s' % (E2SETTINGSPATH, BFNAME)) and os.path.isfile(BOUQUETSPATH):
                      remove_line(BOUQUETSPATH, BFNAME)
           with open(BOUQUETSPATH, 'a') as outfile:
            outfile.write('#SERVICE 1:7:1:0:0:0:0:0:0:0:FROM BOUQUET "%s" ORDER BY bouquet\r\n' % BFNAME)
            outfile.close()
            if LastScanned in line:
                  remove_line(BOUQUETSPATH, LastScanned)
                  with open(BOUQUETSPATH, 'a') as outfile:
                      outfile.write('#SERVICE 1:7:1:0:0:0:0:0:0:0:FROM BOUQUET "%s" ORDER BY bouquet\r\n' % LastScanned)
                      outfile.close()
           os.system('wget -q -O - http://root%s@127.0.0.1/web/servicelistreload?mode=0 > /dev/null 2>&1  && sleep 2')
           return
        else:
           print("File missing %s" % M3UPATH)

将日期从(/tmp/IPTV/IPTV1.m3u)转换为(/etc/enigma2/userbouquet.IPTV1.tv)

我需要的是对(/tmp/IPTV)中的所有文件执行相同的工作,例如(IPTV1、IPTV2、IPTV3…IPTV100) 并将其转换为(/etc/enigma2/userbouquet.IPTV1.tv,/etc/enigma2/userbouquet.IPTV2.tv…/etc/enigma2/userbouquet.IPTV100.tv)

多谢各位


Tags: iniflineetctvtmpoutfilewrite
1条回答
网友
1楼 · 发布于 2024-06-26 00:10:04

好的。。。。我找到了解决办法


M3UPATH= '/tmp/IPTV'
LastScanned= 'userbouquet.LastScanned.tv'
WGET='wget  no-check-certificate'
BOUQUETSPATH='/etc/enigma2/bouquets.tv'
E2SETTINGSPATH='/etc/enigma2'
##################################################################
import os
def convert(m3ufile,BFNAME):
       with open('%s/%s' % (E2SETTINGSPATH, BFNAME), 'w') as outfile:
        desk_tmp = ''
        outfile.write('#NAME %s\r\n' % m3ubase)
        if os.path.isfile(m3ufile):
          for line in open(m3ufile):
            if line.startswith('http://'):
                    outfile.write('#SERVICE 4097:0:1:0:0:0:0:0:0:0:%s' % line.replace(':', '%3a'))
                    outfile.write('#DESCRIPTION %s' % desk_tmp)
            elif line.startswith('#EXTINF'):
                    desk_tmp = '%s' % line.split(',list.alliptvlinks.com ')[-1]
            elif '<stream_url><![CDATA' in line:
                    outfile.write('#SERVICE 4097:0:1:0:0:0:0:0:0:0:%s\r\n' % line.split('[')[-1].split(']')[0].replace(':', '%3a'))
                    outfile.write('#DESCRIPTION %s\r\n' % desk_tmp)
            elif '<title>' in line:
                    if '<![CDATA[' in line:
                        desk_tmp = '%s\r\n' % line.split('[')[-1].split(']')[0]
                    else:
                        desk_tmp = '%s\r\n' % line.split('<')[1].split('>')[1]
          outfile.write('\r\n')           
          outfile.close()
          if os.path.isfile(BOUQUETSPATH):
           for line in open(BOUQUETSPATH):
            if BFNAME in line:
                  if os.path.isfile('%s/%s' % (E2SETTINGSPATH, BFNAME)) and os.path.isfile(BOUQUETSPATH):
                      remove_line(BOUQUETSPATH, BFNAME)
           with open(BOUQUETSPATH, 'a') as outfile:
            outfile.write('#SERVICE 1:7:1:0:0:0:0:0:0:0:FROM BOUQUET "%s" ORDER BY bouquet\r\n' % BFNAME)
            outfile.close()
            if LastScanned in line:
                  remove_line(BOUQUETSPATH, LastScanned)
                  with open(BOUQUETSPATH, 'a') as outfile:
                      outfile.write('#SERVICE 1:7:1:0:0:0:0:0:0:0:FROM BOUQUET "%s" ORDER BY bouquet\r\n' % LastScanned)
                      outfile.close()
           os.system('wget -q -O - http://root%s@127.0.0.1/web/servicelistreload?mode=0 > /dev/null 2>&1  && sleep 2')
           return
        else:
           print("File missing %s" % M3UPATH)       
counter=0
for subdir, dirs, files in os.walk(M3UPATH):
    for file in files:
        
        if file.endswith(".m3u"):
                  counter=counter+1
                  m3ufile=os.path.join(subdir, file)
                  print "m3ufile",m3ufile
                  m3ubase=file[:-4]
                  print "m3ubase",m3ubase
                  BFNAME= 'userbouquet.%s.tv'%m3ubase
                  print "m3ubase",m3ubase
                  convert(m3ufile,BFNAME)
print "%s m3ufiles converted to bouquets"%str(counter)

相关问题 更多 >