zoukankan      html  css  js  c++  java
  • python 批量处理文件

    def GetFileList(dir, fileList,notdeal=[]):
        newDir = dir
        if os.path.isfile(dir):
            fileList.append(dir)
        elif os.path.isdir(dir):
            for s in os.listdir(dir):
                #如果需要忽略某些文件夹,使用以下代码
                if s in notdeal:
                    continue
                newDir = os.path.join(dir, s)
                GetFileList(newDir, fileList,notdeal)
        return fileList
    
    def pushFileinFold(filename,foldname,createpath=False):
        retname=''
        names=filename.split('/')
    
        if foldname[0]=='/':
            retname=foldname
        else:
            for i in range(len(names)-1):
                retname =retname+names[i]+'/'
            retname+=foldname
        if createpath==True:
            if os.path.exists(retname)==False:
                os.makedirs(retname)
                # print(retname+'/'+ names[len(names) - 1])
        return retname +'/'+ names[len(names) - 1]
    
    filepath
    = "/home/b/a" lists = GetFileList(filepath, [],['deal','python']) for i in range(len(lists)):   globals()['data'+str(i)]=pd.read_csv(lists[i],header=None)
      #对数据进行处理
      globals()['data'+str(i)].to_excel(pushFileinFold(lists[i],'deal',True))

    读取/home/b/a文件夹子下的所有文件,里面的deal,python文件夹不处理

    数据处理后保存到 文件所在位置的deal文件夹下,若是文件夹不存在则创建

  • 相关阅读:
    本地快速搭建 FTP 服务器
    css 四个角
    时间
    两个json深度对比
    工作常用
    js模块化 中的变量可在局部 中的‘全局共享’
    datatables 的导出button自定义
    css布局技巧
    datables自定义排序
    js判断是否为空 或者全部为空
  • 原文地址:https://www.cnblogs.com/skycandy/p/9303864.html
Copyright © 2011-2022 走看看