如果python中的某些文件类型,则移动文件并创建目录
发布时间:2020-12-20 13:33:06 所属栏目:Python 来源:网络整理
导读:这可能是一个简单的问题,但我对 python和编程一般都是新手. 我正在研究一个简单的程序,在镜像源位置的目录结构时,将.mp3文件从一个位置复制/移动到另一个位置.到目前为止我的工作,但它也在目标位置创建新的文件夹,即使源文件夹不包含mp3文件.我只想创建新目
这可能是一个简单的问题,但我对
python和编程一般都是新手.
我正在研究一个简单的程序,在镜像源位置的目录结构时,将.mp3文件从一个位置复制/移动到另一个位置.到目前为止我的工作,但它也在目标位置创建新的文件夹,即使源文件夹不包含mp3文件.我只想创建新目录,如果源包含.mp3s,否则它可能会导致目标中的一堆空文件夹. 这是我到目前为止: import os import shutil #Used for copying files ##CONFIG source_dir = "C:UsersusernameDesktopiTunes" #set the root folder that you want to scan and move files from. This script will scan recursively. destPath = "C:UsersusernameDesktopconverted 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 现在它将在目标driectory中创建所有这些文件夹,即使没有.mp3s要复制….. 任何帮助表示赞赏! 解决方法
我能想到的最简单的事情是你的现有代码只是让它跳过任何没有任何.mp3文件的文件夹.这可以通过将以下项和if语句添加到循环顶部来轻松完成.
itertools.ifilter() 和
fnmatch.fnmatch() 功能可以一起使用,以简化对具有适当扩展名的文件的检查.
from itertools import ifilter from fnmatch import fnmatch ext = '.mp3' fnPattern = '*'+ext for dirName,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,fileList in os.walk(source_dir): # generate list of files in directory with desired extension matches = ifilter(lambda fname: fnmatch(fname,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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |