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将Mysql的查询数据导出到文件的方法

mysql官方提供了很多种connector,其中包括python的connector。 下载地址在:http://dev.mysql.com/downloads/connector/p...

Python中一个for循环循环多个变量的示例

首先,熟悉一个函数zip,如下是使用help(zip)对zip的解释。 Help on built-in function zip in module __builtin__: zip...

python字符串和常用数据结构知识总结

python字符串和常用数据结构知识总结

使用字符串 第二次世界大战促使了现代电子计算机的诞生,当初的想法很简单,就是用计算机来计算导弹的弹道,因此在计算机刚刚诞生的那个年代,计算机处理的信息主要是数值,而世界上的第一台电子计...

使用Python压缩和解压缩zip文件的教程

python 的 zipfile 提供了非常便捷的方法来压缩和解压 zip 文件。 例如,在py脚本所在目录中,有如下文件: 复制代码 代码如下:readability/readabil...

Python代码调试的几种方法总结

Python代码调试的几种方法总结

使用 pdb 进行调试 pdb 是 python 自带的一个包,为 python 程序提供了一种交互的源代码调试功能,主要特性包括设置断点、单步调试、进入函数调试、查看当前代码、查看栈片...