python遍历文件

作者: 罗蓁蓁 | 来源:发表于2020-05-01 12:25 被阅读0次

python 操作文件非常简单,却也非常高效!

此次,主要写遍历文件:

#copy the file with directory,and search all of the file's with path
def search_file(src,dst):
    file_list = []
    if src is None:
        raise Exception('source_path is None')
    #if dst is not None:
    #    raise Exception('dest_path has existed')
    print src
    print dst
    shutil.copytree(src,dst)
    for dirpath,dirnames,filenames in os.walk(dst):
        for name in filenames:
            file_list.append(dirpath + '\\' + name)

    return file_list

小小说明

我们操作文件,最好使用shutil这个库,高效方便

而,os.walk也非常好用

小尾巴

出差必备:
买火车票、高铁票、机票,订酒店都打9折的出行工具TRIP,点击注册

优惠购物:
你还在傻傻的原价淘宝吗?来这里领取内部优惠券,折扣力度非常大!点击注册,注册需要邀请码UWD9Q9E。

相关文章

网友评论

    本文标题:python遍历文件

    本文链接:https://www.haomeiwen.com/subject/teafghtx.html