Python:处理本地文件,返回目录文件、文件复制、目录文件复制等

功能:
1、获取目录下所有文件路径
2、指定目录下查找指定文件,存在则返回绝对路径
3、复制多个文件到指定目录
4、将源目录origindir所有文件包括子目录,复制到savedir

# !/usr/bin/env python
# -*-coding:utf-8 -*-
import os,json,shutil,time

def getRootPath():
    """获取项目的根目录"""
    curPath = os.path.abspath(os.path.dirname(__file__))
    rootPath = curPath[:curPath.find("Projectname\\")+len("Projectname\\")]
    return  rootPath

def getOtherPath(relpath):
    """项目Projectname的根目录下,补充相对路径为绝对路径"""
    rootPath = getRootPath()
    abspath = os.path.abspath(rootPath+relpath)
    return abspath

def get_allfilepaths(dirpath):
    """返回目录dirpath下所有文件路径"""
    filepaths = []
    for root, dirs, files in os.walk(dirpath, topdown=True):
        for f in files:
            filepaths.append(os.path.join(root, f))
    filepaths.sort()
    return filepaths

def search_file_abspath(dirpath,filename):
    """在目录dirpath中查找filename文件,存在则返回绝对路径filepath,不存在则返回None"""
    filepath = None
    for root, dirs, files in os.walk(dirpath, topdown=True):
        if filename in files:
            filepath = os.path.join(root, filename)
            break
    return filepath

def copy_files(filepaths,savedir):
    """复制文件到指定目录,将filepaths复制保存到savedir目录中"""
    if not os.path.exists(savedir): # savedir不存在,则递归创建
        os.makedirs(savedir)
    L = len(filepaths)
    start = time.perf_counter()
    for filepath in filepaths:
        i = filepaths.index(filepath) + 1
        filename = os.path.basename(filepath)
        n_filepath = os.path.join(savedir, filename)
        if not os.path.exists(n_filepath):
            shutil.copy(filepath, n_filepath)
        a, b = int(i / L * 50), int((L - i) / L * 50)
        print("\r有效路径共{}个,已复制{}个,进度:{:^3.0f}%<{}>{}{:.2f}s".format(L,i,(i / L) * 100, "▋" * a, "-" * b,time.perf_counter() - start),end="")

def copy_dirs(origindir,savedir):
    """将源目录origindir所有文件包括子目录,复制到savedir"""
    # 获取origindir下所有文件路径
    filepaths = []
    for root, dirs, files in os.walk(origindir, topdown=True):
        for f in files:
            filepaths.append(os.path.join(root, f))

    if not os.path.exists(savedir): # savedir不存在,则递归创建
        os.makedirs(savedir)
    L = len(filepaths)
    start = time.perf_counter()
    for filepath in filepaths:
        i = filepaths.index(filepath) + 1
        relpath = filepath[len(origindir):]
        n_filepath = savedir+relpath
        if not os.path.exists(os.path.dirname(n_filepath)):
            os.makedirs(os.path.dirname(n_filepath))
        if not os.path.exists(n_filepath): # savedir中不存在,copy
            shutil.copy(filepath, n_filepath)
        a, b = int(i / L * 50), int((L - i) / L * 50)
        print("\r有效路径共{}个,已复制{}个,进度:{:^3.0f}%<{}>{}{:.2f}s".format(L,i,(i / L) * 100, "▋" * a, "-" * b,time.perf_counter() - start),end="")

if __name__ == '__main__':
    nasdir = r'F:\origindir'
    savedir = r'F:\savedir'
    copy_dirs(nasdir,savedir)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值