复制文件到一个文件夹并进行排序copy_imgs_to_dir

复制文件到一个文件夹并进行排序copy_imgs_to_dir

文件存储方式

|--parentdir
|----copy_imgs_to_dir.py
|----Video0
|--------xxx0.mp4
|--------xxx1.mp4
|----Video1
|--------xxx0.mp4
|--------xxx1.mp4
|----Video2
|--------xxx0.mp4
|--------xxx1.mp4

创建文件名为:copy_imgs_to_dir.py

import os		#专门用于文件处理
import sys        #system系统处理文件
filedir = os.path.dirname(sys.argv[0])      #获取脚本所在目录   sys.argv[0]表示获取脚本的位置
os.chdir(filedir)       #将脚本所在的目录设置为工作目录
wdir = os.getcwd()        #.getcwd() 可以直接输出
print('当前工作目录:{}\n'.format(wdir))      #打印当前工作目录  输出格式一定要用.format()

import shutil   #高级文档处理

image_dir = 'Images_Repo'        
if not os.path.exists(image_dir):   #检测是否存在image_dir文件夹
        os.mkdir(image_dir)        #创建image_dir文件夹

specified_id = 1            
files = os.listdir(image_dir)
file_idxs = [int(file.split('.')[0]) for file in files]
idx = max(file_idxs) if len(file_idxs) != 0 else 1
idx = max(idx+1, specified_id)

f = open(image_dir +'_info.txt', 'a+')
f.seek(0,0)  #移动到文件头
lines = f.readlines()
print(len(lines))
f.seek(0,2)  #移动到文件尾
existed_img = [os.path.basename(line.split(' ')[0]) for line in lines]    
i, j = 0, 0
for parent, dirs, files in os.walk('DATA'):         #浏览整个DATA文件夹 
	parent_base = os.path.basename(parent)         #parent指的是包含脚本的文件夹
	if ('Picture' in parent_base):                  #每次只能处理一个,要么处理文件夹,要么处理文档
		for file in files:
			if file not in existed_img:
				file_ext = file.split('.')[-1]
				new_name = str(idx).zfill(8) + '.' + file_ext
				image_src = os.path.join( parent, file)          #os.path.join()函数用于路径拼接文件路径,例如:parent/***/file
				image_dst = os.path.join( image_dir, new_name)   
				if not os.path.exists(image_dst):    #os.path.exists() 判断文件是否存在
					shutil.copy(image_src, image_dst)     #将老文件复制到新文件中,老文件-->新文件
					idx += 1
					j += 1
					new_line = image_src + '   ------>   ' +image_dst +'\n'
					f.write(new_line)
					print('本次复制了文件{:25s}到文件{:25s}'.format(file, new_name))     #最大的特点就是不用理会数据类型的问题
			else:
				i += 1
				print('文件{:25s}此前已经被复制'.format(file))
f.close()
print('本次跳过了{:6d}个此前已被复制的文件'.format(i))
print('本次复制了{:6d}个文件'.format(j))
				
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/2160.html原文链接:https://javaforall.net

(0)
上一篇 2020年11月8日 下午9:33
下一篇 2020年11月8日 下午9:33


相关推荐

  • if sql语句_SQL IF语句介绍和概述

    if sql语句_SQL IF语句介绍和概述ifsql语句ThisarticleexplorestheusefulfunctionSQLIFstatementinSQLServer.本文探讨了SQLServer中有用的函数SQLIF语句。介绍(Introduction)Inreallife,wemakedecisionsbasedontheconditions….

    2022年7月16日
    23
  • I2C电平转换电路_i2c电平转换芯片

    I2C电平转换电路_i2c电平转换芯片电平转换电路左侧位从机器件,后侧为单片机(主器件)完整的应用电路图电路图特此记录anlog2021年11月11日

    2022年8月10日
    6
  • idea2021.7.15激活码【2021.7最新】

    (idea2021.7.15激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlMLZPB5EL5Q-eyJsa…

    2022年3月21日
    49
  • CreateCompatibleDC

    CreateCompatibleDCVB声明DeclareFunctionCreateCompatibleDCLib”gdi32″Alias”CreateC

    2022年4月6日
    84
  • FIO详解

    FIO详解fio FlexibleIOTe 安装 a 下载地址 http freshmeat sourceforge net projects fio b 安装两个插件 yuminstallli yuminstallzi c 编译安装 tar xvffio 2 1 10 tar gz cdfio 2 1 10 configure mak

    2026年3月19日
    2
  • JS对象与JSON字符串之间的转换

    JS对象与JSON字符串之间的转换JSON JS 中的对象只有 JS 自己认识 其他的语言都不认识 JSON 就是一个特殊格式的字符串 这个字符串可以被任意的语言所识别 并且可以转换为任意语言中的对象 JSON 在开发中主要用来数据的交互 JSON 和 JS 对象的格式一样 只不过 JSON 字符串中的属性名必须加双引号其他的和 JS 语法一致 JSON 分类 1 对象 2 数组 JSON 中允许的值 1 字符串 2 数值 3 布尔值 4 null5 对象 6 数组将 JSON 字符串转换为 JS 中的对象在 JS

    2026年3月19日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号