python-os库对文件夹的处理

前言

一、删除文件夹指定后缀名的文件

def delete_tif_files(folder_path):
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            if file.endswith('.tif'):
                os.remove(os.path.join(root, file))

if os.path.getsize(result_path):
     delete_tif_files(result_path)

1.os.path.getsize()获取文件大小

2.os.walk()遍历输出path路径下的所有root,dirs,files

![在这里插入图片描述](https://img-blog.csdnimg.cn/4ddafb5333a345c7981e3ce80dabafbe.png
在这里插入图片描述

二、判断文件夹是否存在

if not os.path.exists(resule_path):
	os.mkdir(path)

三、复制文件夹以及子文件夹下所有文件

import os
import shutil

def copy_file(file_path,copy_path):
    if not os.path.exists(copy_path):
        os.mkdir(copy_path)
    os.chdir(file_path)
    print(os.path.abspath(os.curdir))
    all_file = os.listdir()
    for f in all_file:
        if os.path.isdir(f):
            file = os.path.join(copy_path, f)

            if not os.path.exists(file):
                os.mkdir(file)

            file_path_new=os.path.join(file_path,f)
            copy_path_new=os.path.join(copy_path,f)
            check_file(file_path_new,copy_path_new)
            os.chdir(file_path)
        else:

            file_old=os.path.join(file_path,f)
            copy_path_new=os.path.join(copy_path,f)
            if not os.path.isfile(copy_path_new):
                shutil.copy(file_old, copy_path_new)

file_path=r""
copy_path=r''
file_list = copy_file(file_path,copy_path) #待读取的文件夹

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值