python python3查找文件格式(电影,图像)或文件名模式的列表,并将找到的文件移动到指定的文件夹,日志记录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python python3查找文件格式(电影,图像)或文件名模式的列表,并将找到的文件移动到指定的文件夹,日志记录相关的知识,希望对你有一定的参考价值。

#! /usr/bin/python3
# this script run on Linux, change path in accordance with your OS
import shutil
import fnmatch
import os

# images = ['*.jpg', '*.jpeg', '*.png', '*.tif', '*.tiff']
movies = ['*.mov', '*.mp4', '*.avi', '*.flv', '*.wmv','*.MOV', '*.MP4', '*.AVI', '*.FLV', '*.WMV']
#patterns = ['*_????_1.*']  #  regex looking for my duplicated files

rootFolder = '/path/to/your/collection/folder'
mvFolder = '/path/to/your/designated/folder'

matches = []
print('Starting...')
_total_size = 0
for root, dirnames, filenames in os.walk(rootFolder):
    for instance in movies:   #  change moviesto list of patterns or images
        for filename in fnmatch.filter(filenames, instance):
            # print("working on: {}".format(filenames)) # uncomment for monitoring
            _path = os.path.join(root, filename)
            _size = os.path.getsize(_path)  # >> 20
            _total_size += _size
            matches.append(_path + '\t' + str(_size))
            _new_path = os.path.join(mvFolder, filename)
            shutil.move(_path, _new_path)   #  comment out for dry run
            print('Moving {} to {}'.format(filename, _new_path))
# print(matches)
with open(os.path.join(mvFolder, 'log.txt'), 'w') as f: #change log file as desired
    for item in matches:
        f.write(item+'\n')
    f.write("Total found size: {0:.2f} MB".format(_total_size/1024/1024))
print('Done.')

以上是关于python python3查找文件格式(电影,图像)或文件名模式的列表,并将找到的文件移动到指定的文件夹,日志记录的主要内容,如果未能解决你的问题,请参考以下文章

Python3编写网络爬虫04-爬取猫眼电影排行实例

Python3爬取电影信息:调用API

反爬虫2-python3.6 正则表达式抓取猫眼电影TOP100

Python3.5:爬取网站上电影数据

python3 django 电影网站项目

使用 BeautifulSoup 查找网页上的特定文本