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文件夹下,若是文件夹不存在则创建

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM