这可能是一个简单的问题,但我对

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

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

  1. python合并两个文本文件内容_利用python自带插件合并多个excle文件内容

    location = "D:/file/" # 你需要合并该目录下excel文件的指定的文件夹 date = "20171016" # 不需要,笔者在这里使用此 ...

  2. python批量提取word指定内容_使用python批量读取word文档并整理关键信息到excel表格的实例...

    目标 最近实验室里成立了一个计算机兴趣小组 倡议大家多把自己解决问题的经验记录并分享 就像在CSDN写博客一样 虽然刚刚起步 但考虑到后面此类经验记录的资料会越来越多 所以一开始就要做好模板设计(如下 ...

  3. 用python爬取qq空间内容_用python爬取QQ空间

    原博文 2016-11-18 17:19 − 好久没写博文了,最近捣鼓了一下python,好像有点上瘾了,感觉python比js厉害好多,但是接触不久,只看了<[大家网]Python基础教程(第 ...

  4. python正则匹配括号以及内容_【Python】正则表达式匹配最里层括号的内容

    现在有一个字符串: str1 = '(subject_id = "A" OR (status_id = "Open" AND (status_id = &quo ...

  5. python提取txt中指定内容_提取视频中的音频——python三行程序搞定!

    写在开头   身处数据爆炸增长的时代,各种各样的数据都飞速增长,视频数据也不例外.我们可以使用 python 来提取视频中的音频,而这仅仅需要安装一个体量很小的python包,然后执行三行程序!    ...

  6. python提取txt中指定内容_提取视频中的音频,Python三行程序搞定!

    来源:http://suo.im/5yOtbx 写在开头 身处数据爆炸增长的时代,各种各样的数据都飞速增长,视频数据也不例外.我们可以使用 python 来提取视频中的音频,而这仅仅需要安装一个体量很 ...

  7. python提取网页中p标签中的内容_使用Python进行爬虫的初学者指南

    前言 爬虫是一种从网站上抓取大量数据的自动化方法.即使是复制和粘贴你喜欢的网站上的引用或行,也是一种web抓取的形式.大多数网站不允许你保存他们网站上的数据供你使用.因此,唯一的选择是手动复制数据,这 ...

  8. python合并两个文本文件内容_用Python 将两个文件的内容合并成一个新的文件.

    一个文件的内容是: Introduction to Programming, Networking Fundamentals, Internetworking Technologies, Platfo ...

  9. python程序员的工作内容_每个Python程序员都要知道的日志实践

    许多应用程序中都会有日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系统的运行状况进行跟踪.在.NET平台中,有非常著名的第三方开源日志组件log4net,c++中,有人们熟悉的log4cp ...

最新文章

  1. 【超详细】一文学会链表解题
  2. 面试官:连框架都没用熟练,就这还来面试?
  3. 浏览器中的“Linux”
  4. js发送邮件确定email地址
  5. 求一个简单的php购物车,PHP简单实现购物车
  6. 蚂蚁战配基金已售罄两只 累计关注人数超500万
  7. 苹果2020年全系展望:iPhone12不会去掉刘海但5G是必须的
  8. 199. Binary Tree Right Side View
  9. 【 Perl 】三种方式解决” Wide character in print “
  10. duilib之源码分析
  11. 【转】缺陷与出路—一个游戏开发者的反思
  12. 暴走恭亲王:Jed McCaleb才是中本聪大魔王?
  13. 新人Unity下载安装
  14. 2021-09-10 QTdesigner 介绍入门 布局
  15. win10 计算机描述,怎么设置win10以安全描述符定义语言(SDDL)语法表示的计算机访问权限...
  16. 关于CSDN原力计划活动说明
  17. 不写一段代码来获取扇贝单词的接口数据
  18. 别让职场形象毁了你的职场前程
  19. Android 自定义下拉列表
  20. Latex中如何加粗字体 如何打出圆圈序号

热门文章

  1. linux下python安装opencv库_Linux下怎么配置python和opencv
  2. 将oracle导出成文本文件,oracle 数据能否导出成纯文本文件呢?
  3. mysql 二进制日志查看_使用mysqlbinlog从二进制日志文件中查询mysql执行过的sql语句 (原)...
  4. Swift 位运算练习
  5. Echarts开源可视化库学习(三)主题的使用
  6. Python教程:迭代器的正确使用方法
  7. php 多维数组按值排序,按子值对php多维数组排序
  8. html5语义化标签marquee,高效书写HTML5,快速提升你的编码效率!
  9. dw超链接标签_小花园DW学习笔记
  10. 【编译原理】让我们来构建一个简单的解释器(Let’s Build A Simple Interpreter. Part 4.)(python/c/c++版)(笔记)