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