mikeserv
December 8th, 2007, 10:27 PM
I've been reading a few Python books for the past week or so and I'm trying to gain some simple programming skills. I've written a program that will recursively scan through a folder and remember all of the files inside of it.
I think I will eventually use the script to sync my Rockbox iPod, but for now it's just an excercise.
Here's the code:
#!/usr/bin/python
#Filename : Beginnings.py
modules = {}
import sys
import os
import time
import cPickle as p
def mp3_dict(path, mp3_list = {}):
"""Scans a path for all .mp3 files. Returns dictionary 'path:file;file;file"""
folder = [] #defines folder as list
for item in os.listdir(path): #scans path for all files and folders
if item[0] == ".": #ignores hidden files
pass
elif os.path.isfile(path + item) and item[-3:] == "mp3": #checks if item is mp3 file
try: #checks if mp3 is first in folder and adds to value list
mp3_list[path] = mp3_list[path] + ";" + item
except:
mp3_list[path] = item
elif os.path.isdir(path + item + "/"): #checks if item is folder, and adds it to sub
folder.append(path + item + "/") #folder list
[mp3_dict(item, mp3_list) for item in folder] #recursively scans subfolder list
return mp3_list
def dat_write(dat_path, f_name):
"""Pickles return of mp3_dict for dat_path to file f_name"""
f = open(dat_path + f_name, 'w')
p.dump(mp3_dict(dat_path), f)
f.close()
def dat_read(path):
"""Unpickles and returns path."""
f = file(path)
dat = p.load(f)
f.close()
return dat
def dat_print(mp3l, path):
"""Parses out dictionary in format path:file;file;file
and prints readable output."""
print path + ":\n" #prints original search path
for row in mp3l:
print row[len(path):] + ": ", #prints subfolder name
for item in mp3l[row].split(";"): #splits dictionary values into filenames
if item == mp3l[row].split(";")[-1]: #checks if file is last in folder
print "%s\n" %(item) #prints last file and starts new line
else:
print "%s, " %(item), #prints files in subfolder in comma sep list
ipath = '/media/ipod/PodCasts/'
hpath = '/home/mhodgin/PodCasts/'
sfile = 'mp3_list.dat'
dat_print(mp3_dict(ipath), ipath)
dat_print(mp3_dict(hpath), hpath)
#dat_write(hpath, sfile)
#dat_write(ipath, sfile)
#dat_print(dat_read(hpath + sfile), hpath)
#dat_print(dat_read(ipath + sfile), ipath)
The problem I'm having is this: the first (and main) function mp3_dict() scans some folders and saves its files to the dictionary fine, but for some reason it doesn't clear the dictionary afterwards. So when I run it the second time, it returns a compounded list, and this is frustrating. Why isn't the variable clearing?
-Mike
I think I will eventually use the script to sync my Rockbox iPod, but for now it's just an excercise.
Here's the code:
#!/usr/bin/python
#Filename : Beginnings.py
modules = {}
import sys
import os
import time
import cPickle as p
def mp3_dict(path, mp3_list = {}):
"""Scans a path for all .mp3 files. Returns dictionary 'path:file;file;file"""
folder = [] #defines folder as list
for item in os.listdir(path): #scans path for all files and folders
if item[0] == ".": #ignores hidden files
pass
elif os.path.isfile(path + item) and item[-3:] == "mp3": #checks if item is mp3 file
try: #checks if mp3 is first in folder and adds to value list
mp3_list[path] = mp3_list[path] + ";" + item
except:
mp3_list[path] = item
elif os.path.isdir(path + item + "/"): #checks if item is folder, and adds it to sub
folder.append(path + item + "/") #folder list
[mp3_dict(item, mp3_list) for item in folder] #recursively scans subfolder list
return mp3_list
def dat_write(dat_path, f_name):
"""Pickles return of mp3_dict for dat_path to file f_name"""
f = open(dat_path + f_name, 'w')
p.dump(mp3_dict(dat_path), f)
f.close()
def dat_read(path):
"""Unpickles and returns path."""
f = file(path)
dat = p.load(f)
f.close()
return dat
def dat_print(mp3l, path):
"""Parses out dictionary in format path:file;file;file
and prints readable output."""
print path + ":\n" #prints original search path
for row in mp3l:
print row[len(path):] + ": ", #prints subfolder name
for item in mp3l[row].split(";"): #splits dictionary values into filenames
if item == mp3l[row].split(";")[-1]: #checks if file is last in folder
print "%s\n" %(item) #prints last file and starts new line
else:
print "%s, " %(item), #prints files in subfolder in comma sep list
ipath = '/media/ipod/PodCasts/'
hpath = '/home/mhodgin/PodCasts/'
sfile = 'mp3_list.dat'
dat_print(mp3_dict(ipath), ipath)
dat_print(mp3_dict(hpath), hpath)
#dat_write(hpath, sfile)
#dat_write(ipath, sfile)
#dat_print(dat_read(hpath + sfile), hpath)
#dat_print(dat_read(ipath + sfile), ipath)
The problem I'm having is this: the first (and main) function mp3_dict() scans some folders and saves its files to the dictionary fine, but for some reason it doesn't clear the dictionary afterwards. So when I run it the second time, it returns a compounded list, and this is frustrating. Why isn't the variable clearing?
-Mike