Writing files

A

Adonis Vargas

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(outFile)
output = list()
for root, dirs, files in os.walk(self.directory):
files = [x for x in files if x.endswith(".mp3")]
for aFile in sorted(files):
mp3Data = MP3(os.path.join(root, aFile))
title = mp3Data.get("TIT2")
output.append([root, aFile, title])
outCSV.writerows(output)
output = list()
 
K

kyosohma

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(outFile)
output = list()
for root, dirs, files in os.walk(self.directory):
files = [x for x in files if x.endswith(".mp3")]
for aFile in sorted(files):
mp3Data = MP3(os.path.join(root, aFile))
title = mp3Data.get("TIT2")
output.append([root, aFile, title])
outCSV.writerows(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
 
A

Adonis Vargas

-- code --

def _scan(self):
outFile = file("mp3.dat", "wb")
outCSV = csv.writer(outFile)
output = list()
for root, dirs, files in os.walk(self.directory):
files = [x for x in files if x.endswith(".mp3")]
for aFile in sorted(files):
mp3Data = MP3(os.path.join(root, aFile))
title = mp3Data.get("TIT2")
output.append([root, aFile, title])
outCSV.writerows(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
 
J

Jerry Hill

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top