0

I am trying to move files to a corresponding folder based on their filename in Python. There are 3 identifiers, the genre i.e. "DRAMA", the director i.e. "von Trier", and the movie name i.e. "Melancholia". So the file "DRAMA von Trier Melancholia" would need to be moved to say, C:/shared/com/movies/DRAMA/Melancholia/von Trier. There are many files located that need to be moved, so I would rather not hard code the locations/do it 1 by 1. Here is what I have so far which lists all the files I want moved, but does not actually move them.

import os
import shutil
import fnmatch

for dirpath, dirs, files in os.walk('C:/shared/com/movies/'):
    print dirpath
    print dirs
    print files
    for filename in files:
        if filename.endswith('*.mov'):
            shutil.move('') #not sure how to code this to have it move based on paramaters rather than hard coding it
4

1 回答 1

0

您可以尝试使用 modul os.path 并使用 split 或 splittext 获取有关文件名的信息。之后,您可以使用 modul re 拆分文件名。使用部件和 mkdir,您可以创建必要的目录。你能举一个文件名的例子吗?

于 2013-01-31T16:01:24.350 回答