zoukankan      html  css  js  c++  java
  • 【Py】Python的调包日常——文件操作篇

    遍历一个文件夹下所有文件(文件夹可以嵌套)

    import os 
    
    basepath = './'
    
    def get_file_from_path(path):
          if os.path.isdir(path):
                # 处理这个文件夹
                for item in os.listdir(path):
                      next_path=os.path.join(path, item)
                      get_file_from_path(next_path)
                print("All item in dir_path", next_path, " has been processed")
          else:
                # 处理这个文件
    

    复制文件

    from shutil import copyfile
    
    copyfile(frompath,topath)
    

    写文件

    fp = open(filepath, 'a')
    fp.write(stringtowrite)
    fp.close()
    

    读文件

    fp = open(filepath, 'r')
    lines=fp.readlines()
    for line in lines:
          #处理每一行
    fp.close()
    

    新建文件夹

    if not os.path.exists(dir_path):
          os.mkdir(dir_path) #存在会报错
    
  • 相关阅读:
    2017-2018-1 20145237、20155205、20155218实验一 开发环境的熟悉
    作业三总结
    作业二总结
    作业总结1
    自我介绍
    计科16-4刘悦
    第九次作业
    作业八
    作业七
    作业六
  • 原文地址:https://www.cnblogs.com/Ryan16231112/p/13144445.html
Copyright © 2011-2022 走看看