日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python移动文件中某个内容_如果python中的某些文件类型,则移动文件并创建目录...

發布時間:2025/3/19 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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中的某些文件类型,则移动文件并创建目录...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。