I have a bunch of files in a single directory that I would like to organize in sub-directories.
This directory structure (which file would go in which directory) is specified in a file list that looks like this:
Directory: Music\
-> 01-some_song1.mp3
-> 02-some_song2.mp3
-> 03-some_song3.mp3
Directory: Images\
-> 01-some_image1.jpg
-> 02-some_image2.jpg
......................
I was thinking of extracting the data (directory name and file name) and store it in a dictionary that would look like this:
dictionary = {'Music': (01-some_song1.mp3, 02-some_song2.mp3,
03-some_song3.mp3),
'Images': (01-some_image1.jpg, 02-some_image2.jpg),
......................................................
}
After that I would copy/move the files in their respective directories.
I already extracted the directory names and created the empty dirs.
For the dictionary values I tried to get a list of lists by doing the following:
def get_values(file):
values = []
tmp = []
pattern = re.compile(r'^-> (.+?)$')
for line in file:
if line.strip().startswith('->'):
match = re.search(pattern, line.strip())
if match:
tmp.append(match.group(1))
elif line.strip().startswith('Directory'):
values.append(tmp)
del tmp[:]
return values
This doesn't seem to work. Each list from the values
list contains the same 4 file names over and over again.
What am I doing wrong?
I would also like to know what are the other ways of doing this whole thing? I'm sure there's a better/simpler/cleaner way.