Writing files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adonis Vargas

    #1

    Writing files

    I am writing a program that walks a directory full of mp3s reads their
    ID3 data (using Mutagen), this part works perfectly. The problem is I
    write these tags to a CSV file through the CSV module. But when I read
    the file the file seems to be incomplete. Further inspecting it has
    seemed to have stopped writing to the file at a certain point. Something
    in my code? a bug?

    System: Linux 2.4.31 (Slackware), Python 2.5c1

    Any help is greatly appreciated.

    Adonis

    -- code --

    def _scan(self):
    outFile = file("mp3.dat", "wb")
    outCSV = csv.writer(outF ile)
    output = list()
    for root, dirs, files in os.walk(self.di rectory):
    files = [x for x in files if x.endswith(".mp 3")]
    for aFile in sorted(files):
    mp3Data = MP3(os.path.joi n(root, aFile))
    title = mp3Data.get("TI T2")
    output.append([root, aFile, title])
    outCSV.writerow s(output)
    output = list()
  • kyosohma@gmail.com

    #2
    Re: Writing files

    On Mar 19, 2:20 pm, Adonis Vargas <ado...@REMOVET HISearthlink.ne t>
    wrote:
    I am writing a program that walks a directory full of mp3s reads their
    ID3 data (using Mutagen), this part works perfectly. The problem is I
    write these tags to a CSV file through the CSV module. But when I read
    the file the file seems to be incomplete. Further inspecting it has
    seemed to have stopped writing to the file at a certain point. Something
    in my code? a bug?
    >
    System: Linux 2.4.31 (Slackware), Python 2.5c1
    >
    Any help is greatly appreciated.
    >
    Adonis
    >
    -- code --
    >
    def _scan(self):
    outFile = file("mp3.dat", "wb")
    outCSV = csv.writer(outF ile)
    output = list()
    for root, dirs, files in os.walk(self.di rectory):
    files = [x for x in files if x.endswith(".mp 3")]
    for aFile in sorted(files):
    mp3Data = MP3(os.path.joi n(root, aFile))
    title = mp3Data.get("TI T2")
    output.append([root, aFile, title])
    outCSV.writerow s(output)
    output = list()
    Are you closing the file before you try to read it? Other than that,
    I'm drawing a blank with just this sample to work with. Maybe someone
    else will know...or you could post more code?

    Mike

    Comment

    • Adonis Vargas

      #3
      Re: Writing files

      kyosohma@gmail. com wrote:
      <snip>
      >>
      >-- code --
      >>
      > def _scan(self):
      > outFile = file("mp3.dat", "wb")
      > outCSV = csv.writer(outF ile)
      > output = list()
      > for root, dirs, files in os.walk(self.di rectory):
      > files = [x for x in files if x.endswith(".mp 3")]
      > for aFile in sorted(files):
      > mp3Data = MP3(os.path.joi n(root, aFile))
      > title = mp3Data.get("TI T2")
      > output.append([root, aFile, title])
      > outCSV.writerow s(output)
      > output = list()
      >
      Are you closing the file before you try to read it? Other than that,
      I'm drawing a blank with just this sample to work with. Maybe someone
      else will know...or you could post more code?
      >
      Mike
      >

      Actually, I re-ran this in a terminal and it worked perfectly. I was
      using IDLE to write this code, kinda peculiar. Maybe something to do
      with IDLE and CSV (or writing to files) with lines ~1000. A socket
      timing out maybe?

      Thanks anyways.

      Adonis

      Comment

      • Jerry Hill

        #4
        Re: Writing files

        On 3/19/07, Adonis Vargas <adonis@removet hisearthlink.ne twrote:
        Actually, I re-ran this in a terminal and it worked perfectly. I was
        using IDLE to write this code, kinda peculiar. Maybe something to do
        with IDLE and CSV (or writing to files) with lines ~1000. A socket
        timing out maybe?
        It's because your file never got closed. When you ran the script from
        the terminal, your file was automatically closed when outCSV was
        garbage collected. When you ran your script in IDLE outCSV was still
        a live variable, so it wasn't garbage collected automatically. Since
        you never told it to close the file, there was still data sitting in
        the buffer waiting to be written.

        --
        Jerry

        Comment

        Working...