加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python扫描目录读取文件,不考虑子目录

发布时间:2020-12-17 17:48:39 所属栏目:Python 来源:网络整理
导读:感兴趣Python扫描目录读取文件,不考虑子目录的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。 # *** 网 (jb51.cc) ***try: source_dir = '/dir/source/' target_dir = '/dir/old/' for root,sub_dirs,files in os.walk(source_dir): for special_fil
感兴趣Python扫描目录读取文件,不考虑子目录的小伙伴,下面一起跟随编程之家 52php.cn的小编来看看吧。
#  *** 网 (52php.cn) ***
try:
    source_dir = '/dir/source/'
    target_dir = '/dir/old/'
    for root,sub_dirs,files in os.walk(source_dir):
        for special_file in files:
            spcial_file_dir = os.path.join(root,special_file)
            # 打开文件的两种方式
            # 1.文件以绝对路径方式
            with open(spcial_file_dir) as source_file:
            # 2.文件以相对路径方式
            # with open(r'dir_test/test.txt') as source_file:
                for line in source_file:
                    # do something
            # 移动文件
            shutil.move(spcial_file_dir,target_dir)
            logger.info(u'文件%s移动成功'% spcial_file_dir)
    return HttpResponse(json.dumps({'result':0}))
except Exception as e:
    # do something
 

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读