python根据文件名中的文本字符将多个文件从一个文件夹移动到另一个文件夹 [英] python moving multiple files from one folder to the other based on text characters in file name

查看:62
本文介绍了python根据文件名中的文本字符将多个文件从一个文件夹移动到另一个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Python 很陌生.我一直在探索 shutil 模块并且可以移动一般的东西.我的问题围绕:

I'm quite new to Python. I've been exploring the shutil module and can move things in general. My question revolves around:

想象一个场景,其中一个导出文件夹中有数百个文件.虽然所有文件都是不同的,但每个文件中有 13 个是针对特定供应商的.我想创建一个脚本,通过导出文件夹,评估每个文件名,抓取所有 Apple 文件并将它们放入 Apple 文件夹、Intel 文件并将它们放入 Intel 文件夹等.任何智慧将不胜感激.

Imagine a scenario in which you have hundreds of files in an export folder. While all the files are distinct, 13 of each are for a specific vendor. I would like to create a script that goes through the export folder, evaluates each file name, grabs all the Apple files and puts them in the Apple Folder, Intel files and puts them in the Intel Folder, etc. Any wisdom would be greatly appreciated.

我试图在 shutil 副本中使用通配符,但没有任何运气.

I was trying to have wildcards in the shutil copy, but did not have any luck.

谢谢,

JT

推荐答案

我能想到的最简单的解决方案:

Easiest solution I could think of:

import shutil
import os

source = '/path/to/source_folder'
dest1 = '/path/to/apple_folder'
dest2 = '/path/to/intel_folder'

files = os.listdir(source)

for f in files:
    if (f.startswith("Apple") or f.startswith("apple")):
        shutil.move(f, dest1)
    elif (f.startswith("Intel") or f.startswith("intel")):
        shutil.move(f, dest2)

目标文件夹确实需要存在.

这篇关于python根据文件名中的文本字符将多个文件从一个文件夹移动到另一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆