python移动文件中某个内容_如果python中的某些文件类型,则移动文件并创建目录...
這可能是一個(gè)簡(jiǎn)單的問(wèn)題,但我對(duì)
python和編程一般都是新手.
我正在研究一個(gè)簡(jiǎn)單的程序,在鏡像源位置的目錄結(jié)構(gòu)時(shí),將.mp3文件從一個(gè)位置復(fù)制/移動(dòng)到另一個(gè)位置.到目前為止我的工作,但它也在目標(biāo)位置創(chuàng)建新的文件夾,即使源文件夾不包含mp3文件.我只想創(chuàng)建新目錄,如果源包含.mp3s,否則它可能會(huì)導(dǎo)致目標(biāo)中的一堆空文件夾.
這是我到目前為止:
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"
所以如果文件夾結(jié)構(gòu)是這樣的:
root->
band 1->
album name->
song.m4a,
song2.m4a
現(xiàn)在它將在目標(biāo)driectory中創(chuàng)建所有這些文件夾,即使沒有.mp3s要復(fù)制…..
任何幫助表示贊賞!
我能想到的最簡(jiǎn)單的事情是你的現(xiàn)有代碼只是讓它跳過(guò)任何沒有任何.mp3文件的文件夾.這可以通過(guò)將以下項(xiàng)和if語(yǔ)句添加到循環(huán)頂部來(lái)輕松完成.
itertools.ifilter()和
fnmatch.fnmatch()功能可以一起使用,以簡(jiǎn)化對(duì)具有適當(dāng)擴(kuò)展名的文件的檢查.
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),以確保在需要將文件復(fù)制到目標(biāo)的相應(yīng)子分支時(shí)創(chuàng)建先前迭代跳過(guò)的所有子目錄.目錄.
您可以通過(guò)創(chuàng)建和保存可能具有擴(kuò)展名的匹配文件的空迭代器來(lái)稍微優(yōu)化一些事情,然后再次使用它來(lái)確定要復(fù)制的文件:
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
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的python移动文件中某个内容_如果python中的某些文件类型,则移动文件并创建目录...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python手机销售系统详细设计_数据库
- 下一篇: 消防信号总线原理_建筑电气消防设计6大常