python移动文件中某个内容_如果python中的某些文件类型,则移动文件并创建目录...
這可能是一個簡單的問題,但我對
python和編程一般都是新手.
我正在研究一個簡單的程序,在鏡像源位置的目錄結構時,將.mp3文件從一個位置復制/移動到另一個位置.到目前為止我的工作,但它也在目標位置創建新的文件夾,即使源文件夾不包含mp3文件.我只想創建新目錄,如果源包含.mp3s,否則它可能會導致目標中的一堆空文件夾.
這是我到目前為止:
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
現在它將在目標driectory中創建所有這些文件夾,即使沒有.mp3s要復制…..
任何幫助表示贊賞!
我能想到的最簡單的事情是你的現有代碼只是讓它跳過任何沒有任何.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
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的python移动文件中某个内容_如果python中的某些文件类型,则移动文件并创建目录...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python手机销售系统详细设计_数据库
- 下一篇: notepadpython插件_Note