2024-10-03 23:20:45 发布
网友
搜索将文件移动到基于文件名创建的方向的python脚本。在
例如 文件名是ARC20180810185310.jpg 将该文件移到:2018>08
ARC20180810185310.jpg
2018>08
文件名是ARC20180910185310.jpg 将该文件移到:2018>09
ARC20180910185310.jpg
2018>09
首先,您需要glob.glob()函数来查找给定目录中的所有文件:
files = glob.glob('ARC*.jpg')
然后需要提取文件名的某些部分:
将os.makedirs()与exist_ok=True一起使用并创建目录:
os.makedirs(os.path.join(BASE_DIR, year, month))
然后使用shutil.move将文件移动到特定目录
shutil.move(filename, os.path.join(BASE_DIR, year, month, filename))
最后你会得到这样的结果:
import glob import os.path import shutil BASE_DIR = os.path.dirname(os.path.abspath(__file__)) files = glob.glob('ARC*.jpg') for filename in files: year = filename[3:7] month = filename[7:9] try: os.makedirs(os.path.join(BASE_DIR, year, month)) except OSError: pass shutil.move(filename, os.path.join(BASE_DIR, year, month, filename))
只需替换srcDir和dstDir,您将获得文件.jpg如果使用MoveFile(srcDir,dstDir,resuive=True),则将文件.jpg也在srcDir的子目录中。在
from __future__ import print_function import os, re, shutil class MoveFile(object): def __init__(self, srcDir, dstDir, recursive=False, flag='.JPG'): self.srcDir = srcDir self.dstDir = dstDir self.recursive = recursive self.flag = flag self.duplicateFileName = [] self.badFileName = [] self.jpgFile = [] self.srcDirDict = {} def findAllJPG(self): # recursively find file if self.recursive == False: for item in os.listdir(self.srcDir): if os.path.isfile(os.path.join(self.srcDir,item)) and \ os.path.splitext(item)[-1] == self.flag.lower(): self.jpgFile.append(item) else: for root, dirs, files in os.walk(self.srcDir): for item in files: if os.path.splitext(item)[-1] == self.flag.lower(): self.jpgFile.append(item) self.srcDirDict[item] = root if not self.jpgFile: print('NOT FIND ANY JPG FILE!') return self.jpgFile def parse(self, text): try: pat =re.compile('[a-zA-Z]+([\d]+)') match = pat.match(text) data = match.group(1) fileName = data[:4]+'-'+data[4:6] except TypeError: self.badFileName.append(text) fileName = None return fileName def move(self, text): try: fileName = self.parse(text) if fileName == None: return if not os.path.isdir(os.path.join(self.dstDir, fileName)): os.mkdir(os.path.join(self.dstDir,fileName)) srcPath= os.path.join(self.srcDirDict[text], text) dstDir = os.path.join(self.dstDir, fileName) shutil.move(srcPath, dstDir) except: self.duplicateFileName.append(text) raise @staticmethod def decC(dir): return os.path.join(self.srcDir,dir) def run(self): try: if not os.path.isdir(self.dstDir): os.mkdir(self.dstDir) for text in self.findAllJPG(): self.move(text) print('MOVE SUCCESSFUL!') except: raise srcDir = r'C:\Users\Administrator\Desktop\2' dstDir = r'C:\Users\Administrator\Desktop\3' fmv = MoveFile(srcDir, dstDir, recursive = False) fmv.run()
最好先自己尝试一下,这样你就能学到更多。在
我会尝试:
import os from shutil import copyfile folder_path = '*folder path*' os.chdir(folder_path) for file in os.listdir(folder_path) year = file[3:7] month = file[7:9] final_path = folder_path + '/' + year + '>' + month + '/' + file + .jpg' copyfile(file, final_path)
只需将**中的路径替换为所需的内容。在
通过这种方式,您可以将文件名切片并从中获取年和月(字符从3到6和7,8),然后将copyfile复制到包含所切片的年和月的路径。在
首先,您需要glob.glob()函数来查找给定目录中的所有文件:
然后需要提取文件名的某些部分:
^{pr2}$将os.makedirs()与exist_ok=True一起使用并创建目录:
然后使用shutil.move将文件移动到特定目录
最后你会得到这样的结果:
只需替换srcDir和dstDir,您将获得文件.jpg如果使用MoveFile(srcDir,dstDir,resuive=True),则将文件.jpg也在srcDir的子目录中。在
最好先自己尝试一下,这样你就能学到更多。在
我会尝试:
只需将**中的路径替换为所需的内容。在
通过这种方式,您可以将文件名切片并从中获取年和月(字符从3到6和7,8),然后将copyfile复制到包含所切片的年和月的路径。在
相关问题 更多 >
编程相关推荐