使用 python 将文件复制到文件列表中指定的目录
我在一个目录中有一堆文件,我想将它们组织在子目录中。
此目录结构(哪个文件将位于哪个目录中)在文件列表中指定,如下所示:
Directory: Music\
-> 01-some_song1.mp3
-> 02-some_song2.mp3
-> 03-some_song3.mp3
目录:Images\
-> 01-some_image1.jpg
-> 02-some_image2.jpg
................................
我正在考虑提取数据(目录名称和文件name)并将其存储在如下所示的字典中:
dictionary = {'Music': (01-some_song1.mp3, 02-some_song2.mp3,
03-some_song3.mp3),
'Images': (01-some_image1.jpg, 02-some_image2.jpg),
......................................................
}
之后,我将复制/移动文件到各自的目录中。
我已经提取了目录名称并创建了空目录。
对于字典值,我尝试通过执行以下操作来获取列表的列表:
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
这似乎不起作用。 values
列表中的每个列表都一遍又一遍地包含相同的 4 个文件名。
我究竟做错了什么?
我还想知道完成这一切的其他方法是什么? 我确信有更好/更简单/更干净的方法。
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'^-> (.+?)
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.
)
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为原因是您始终重复使用相同的列表。
del tmp[:]
清除列表并且不创建新实例。 在您的情况下,您需要通过调用tmp = []
创建一个新列表以下修复应该有效(我没有测试它)
I think that the cause is that you are reusing always the same list.
del tmp[:]
clears the list and doesn't create a new instance. In your case, you need to create a new list by callingtmp = []
Following fix should work (I didn't test it)
不需要使用正则表达式
输出
no need to use regular expression
output
如果您使用 collections.defaultdict(list),您将获得一个列表,该列表的元素是列表的字典。 如果找不到该键,则会添加一个空列表值,这样您就可以立即开始追加到列表中。 这就是这一行的作用:
如果目录名不存在,它将创建该目录名作为键,并将找到的文件名附加到列表中。
顺便说一句,如果您在使正则表达式正常工作时遇到问题,请尝试使用调试标志创建它们。 我不记得符号名称,但数字是 128。所以如果你这样做:
你会得到这个额外的输出:
并且你可以看到有一个起始行匹配加上 '->; ' (对于 45 62 32),然后重复任何模式和行尾匹配。 对于调试非常有用。
代码:
结果:
) with open(file) as f: for line in f: line = line.strip() match = dir_regex.search(line) if match: dir = match.group(1) else: match = file_regex.search(line) if match: d[dir].append(match.group(1)) return d if __name__ == '__main__': d = get_values('test_file') for k, v in d.items(): print k, v结果:
, 128)你会得到这个额外的输出:
并且你可以看到有一个起始行匹配加上 '->; ' (对于 45 62 32),然后重复任何模式和行尾匹配。 对于调试非常有用。
代码:
结果:
If you use collections.defaultdict(list), you get a list that dictionary whose elements are lists. If the key is not found, it is added with a value of empty list, so you can start appending to the list immediately. That's what this line does:
It creates the directory name as a key if it does not exist and appends the file name found to the list.
BTW, if you are having problems getting your regexes to work, try creating them with the debug flag. I can't remember the symbolic name, but the number is 128. So if you do this:
You get this additional output:
And you can see that there is a start line match plus '-> ' (for 45 62 32) and then a repeated any pattern and end of line match. Very useful for debugging.
Code:
Result:
) with open(file) as f: for line in f: line = line.strip() match = dir_regex.search(line) if match: dir = match.group(1) else: match = file_regex.search(line) if match: d[dir].append(match.group(1)) return d if __name__ == '__main__': d = get_values('test_file') for k, v in d.items(): print k, vResult:
, 128)You get this additional output:
And you can see that there is a start line match plus '-> ' (for 45 62 32) and then a repeated any pattern and end of line match. Very useful for debugging.
Code:
Result: