Python教程

python 批量文件管理

本文主要是介绍python 批量文件管理,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

先上代码,之后再补充

import os
import glob
import shutil

dir='....'

if not os.path.exists(dir+'training'):
    os.mkdir(dir+'training')

if not os.path.exists(dir+'testing'):
    os.mkdir(dir+'testing')


for i in range(1,38):
    a='person{}'.format(i)
    file_name=dir+'training/'+a
    if not os.path.exists(file_name):
        os.mkdir(file_name)

for i in range(38,41):
    a='person{}'.format(i)
    file_name=dir+'testing/'+a
    if not os.path.exists(file_name):
        os.mkdir(file_name)


for j in range(1,10):
    s='s0{}*'.format(j)
    t='person{}'.format(j)
    s_file_name=dir+s
    t_file_name=dir+'training'+'/'+t+'/'
    for data in glob.glob(s_file_name):
        if not os.path.isdir(data):
            shutil.move(data,t_file_name)

for j in range(10,38):
    s='s{}*'.format(j)
    t='person{}'.format(j)
    s_file_name=dir+s
    t_file_name=dir+'training'+'/'+t+'/'
    for data in glob.glob(s_file_name):
        if not os.path.isdir(data):
            shutil.move(data,t_file_name)

for j in range(38,41):
    s='s{}*'.format(j)
    t='person{}'.format(j)
    s_file_name=dir+s
    t_file_name=dir+'testing'+'/'+t+'/'
    for data in glob.glob(s_file_name):
        if not os.path.isdir(data):
            shutil.move(data,t_file_name)
这篇关于python 批量文件管理的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!