Search code examples
pythonregexweb-scraping

writing to a m3u file with python


I did this script I try to write what I request with regex when it comes to writing to a file 'w' writes only last entry I try ('w', 'wb', 'w+') all off them writes last entry where I do wrong?

#-*- coding: utf-8 -*-
import urllib2,urllib
import re
import os
import sys

value=[]
url='https://www.youtube.com/feeds/videos.xmlchannel_id=UCHXdylbsyDVN4UO2Fv8Cgm&API'
req = urllib2.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.75.14 (KHTML,like Gecko) Version/7.0.3 Safari/573.75.14')
response = urllib2.urlopen(req)
link=response.read()
response.close()
match=re.compile('<entry>\n  <id>.+?</id>\n  <yt:videoId>(.+?)</yt:videoId>\n  <yt:channelId>.+?</yt:channelId>\n  <title>(.*?)\(HD\)</title>').findall(link)
for videoid,isim in match:
    #print videoid,isim

    name='#EXTINF:-1 ,'+isim+'\n'
    link='plugin://plugin.video.youtube/play/?video_id='+videoid+'\n'
    value.append((str(name), str(link)))   

    for name,link in value:
        #print name,link
        with open('C:\\Users\\dir\\Desktop\\muhtesem_yuzyil_kosem.m3u', 'wb+') as f:
            f.write(name)
            f.write(link)
            f.seek(0)  # Important: return to the top of the file before reading, otherwise you'll just read an empty string
            data = f.read() # Returns 'somedata\n'
            f.flush()
            f.close()

Solution

  • There are few issues with your data writing code:

    1. You open file in loop, for every value item found
    2. You open file for writing only
    3. You deliberately change internal filehandle position to beginning of file on each iteration, only to read entire file and flush what was left. This is not dangerous, only unnecessary. But you could take some time to review what you know about file operations.
    4. You use with statement, which automatically closes filehandle, but then call close() nevertheless

    There is nothing wrong with opening file for writing only if you do it once, and then loop over your values list:

    with open('C:\\Users\\dir\\Desktop\\muhtesem_yuzyil_kosem.m3u', 'wb') as f:
           for name,link in value:
                f.write(name)
                f.write(link)
    

    Or you can open file on each iteration, but then make sure that you open file for reading and writing:

     for name,link in value:
            with open('C:\\Users\\dir\\Desktop\\muhtesem_yuzyil_kosem.m3u', 'r+b') as f:
                f.seek(0)  # Important: return to the top of the file before reading, otherwise you'll just read an empty string
                data = f.read() # Returns 'somedata\n'
                # make sure that data is written only after cursor was positioned after current file content
                f.write(name)
                f.write(link)