python实现将文件夹下面的不是以py文件结尾的文件都过滤掉的方法

yipeiwu_com6年前Python基础

如下所示:

dir_in = os.path.join(os.path.dirname(__file__), r"oldApp")
    dir_in = unicode(dir_in, r"GBK")
    dir_out = os.path.join(os.path.dirname(__file__), r"newApp")
    dir_out = unicode(dir_out, r"GBK")


    rediret_file_path_list = []
    soure_file_path_out_list = []
    for root, dirs, files in os.walk(dir_in):
        for file in files:
            # print('root=%s' %root)
            # print('1111 file=%s' %file)
            # filter file extend name not .py
            filter_file = file.split('.')
            if filter_file[1] != 'py':
                continue


            soure_file_path_out = os.path.join(root, file)
            # print(soure_file_path_out)
            soure_file_path_out_list.append(soure_file_path_out)
            root_new = root.replace(r'oldApp', r'newApp')
            if not os.path.exists(root_new):
                os.makedirs(root_new)
            rediret_file_path = os.path.join(root_new, file)
            # print('rediret_file_path=%s' %rediret_file_path)
            rediret_file_path_list.append(rediret_file_path)

以上这篇python实现将文件夹下面的不是以py文件结尾的文件都过滤掉的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python之时间和日期使用小结

Python之时间和日期使用小结

对于日期的操作可以说是比较常见的case了,日期与格式化字符串互转,日期与时间戳互转,日期的加减操作等,下面主要介绍下常见的需求场景如何实现 1. 基本包引入 主要需要引入时间和日期的处...

docker django无法访问redis容器的解决方法

docker django无法访问redis容器的解决方法

docker-compose.yal文件中: redis: image: redis container_name: xdemo.redis ports: - 6...

pyqt 实现QlineEdit 输入密码显示成圆点的方法

pyqt 实现QlineEdit 输入密码显示成圆点的方法

使用自带的函数就可以实现: lineEdit.setEchoMode(QLineEdit.Password) import struct from PyQt5.QtWidgets i...

Python+Turtle动态绘制一棵树实例分享

Python+Turtle动态绘制一棵树实例分享

本文实例主要是对turtle的使用,实现Python+turtle动态绘制一棵树的实例,具体代码: # drawtree.py from turtle import Turtle...

Python基础学习之基本数据结构详解【数字、字符串、列表、元组、集合、字典】

Python基础学习之基本数据结构详解【数字、字符串、列表、元组、集合、字典】

本文实例讲述了Python基础学习之基本数据结构。分享给大家供大家参考,具体如下: 前言 相比于PHP,Python同样也是脚本解析语言,所以在使用Python的时候,变量和数据结构相对...