在Python中移动文件并创建特定文件类型的目录

4 投票
3 回答
9688 浏览
提问于 2025-04-17 15:57

这可能是个简单的问题,但我对Python和编程都还是新手。

我正在做一个简单的程序,目的是把.mp3文件从一个地方复制或移动到另一个地方,同时保持源位置的文件夹结构。到目前为止,我的程序是可以工作的,但它在目标位置也会创建新的文件夹,即使源文件夹里没有.mp3文件。我只想在源文件夹里有.mp3文件的情况下才创建新的文件夹,否则目标位置会出现一堆空文件夹。

这是我目前的代码:

import os
import shutil #Used for copying files

##CONFIG
source_dir = "C:\Users\username\Desktop\iTunes\\" #set the root folder that you want to     scan and move files from.  This script will scan recursively.
destPath = "C:\Users\username\Desktop\converted From iTunes" #set the destination root that you want to move files to.  Any non-existing sub directories will be created.
ext = ".mp3" #set the type of file you want to search for.
count = 0 #initialize counter variable to count number of files moved
##

##FIND FILES
for dirName, subdirList, fileList in os.walk(source_dir):

    #set the path for the destination folder(s)
    dest = destPath + dirName.replace(source_dir, '\\') 

    #if the source directory doesn't exist in the destination folder
    #then create a new folder
    if not os.path.isdir(dest):
        os.mkdir(dest)
        print('Directory created at: ' + dest)

    for fname in fileList:
        if fname.endswith(ext) :
            #determine source & new file locations
            oldLoc = dirName + '\\' + fname
            newLoc = dest + '\\' + fname

            if os.path.isfile(newLoc): # check to see if the file already exists.  If it does print out a message saying so.
                print ('file "' + newLoc + fname + '" already exists')

            if not os.path.isfile(newLoc): #if the file doesnt exist then copy it and print out confirmation that is was copied/moved
                try:
                    shutil.move(oldLoc, newLoc)
                    print('File ' + fname + ' copied.')
                    count = count + 1
                except IOError:
                    print('There was an error copying the file:  "' + fname + '"')
                    print 'error'            

print "\n"
print str(count) + " files were moved."
print "\n"

假设文件夹结构是这样的:

root->
 band 1->
  album name->
   song.m4a,
   song2.m4a

现在即使没有.mp3文件要复制,它也会在目标目录中创建所有这些文件夹……

任何帮助都非常感谢!

3 个回答

0

你是不是觉得shutils.copytree可以用更少的代码行来完成你想做的事情呢?

1

我觉得我会为这种情况自己写一个包装函数,来处理复制的操作:

def fcopy(src,dest):
    """
    Copy file from source to dest.  dest can include an absolute or relative path
    If the path doesn't exist, it gets created
    """
    dest_dir = os.path.dirname(dest)
    try:
        os.makedirs(dest_dir)
    except os.error as e:
        pass #Assume it exists.  This could fail if you don't have permissions, etc...
    shutil.copy(src,dest)

现在你可以遍历这个树形结构,对任何 .mp3 文件调用这个函数了。

0

对于你现有的代码,我想到的最简单的方法就是让它跳过那些没有.mp3文件的文件夹。你可以通过在循环的开头添加一些内容和一个if语句来轻松实现这一点。itertools.ifilter()fnmatch.fnmatch()这两个函数可以一起使用,帮助你更简单地检查文件是否有正确的后缀名。

from itertools import ifilter
from fnmatch import fnmatch

ext = '.mp3'
fnPattern = '*'+ext

for dirName, subdirList, fileList in os.walk(source_dir):
    if not any(ifilter(lambda fname: fnmatch(fname, fnPattern), fileList)):
        print '  skipping "{}"'.format(dirName)
        continue
    ...

你还需要把代码中os.mkdir(dest)改成os.makedirs(dest),这样可以确保在之前的循环中跳过的任何子目录在需要复制文件到目标目录的相应子文件夹时能够被创建。

你还可以通过创建并保存一个可能为空的匹配文件的迭代器来稍微优化一下,然后再用它来决定要复制哪些文件:

from itertools import ifilter
from fnmatch import fnmatch

ext = '.mp3'
fnPattern = '*'+ext

for dirName, subdirList, fileList in os.walk(source_dir):

    # generate list of files in directory with desired extension
    matches = ifilter(lambda fname: fnmatch(fname, fnPattern), fileList)

    # skip subdirectory if it does not contain any files of interest
    if not matches:
        continue
    ...
    ... create destination directory with os.makedirs()
    ...
    #  copy each file to destination directory
    for fname in matches:
      ... copy file

撰写回答