python實(shí)現(xiàn)將多個(gè)文件分配到多個(gè)文件夾的方法
如下所示:
import os import shutil #path of imgr path = 'D:\\BaiduNetdiskDownload\\newim\\' #path of folder folderPath = 'D:\\BaiduNetdiskDownload\\folderSort\\' peopleNumber = 61 #new 61 folder numbers as sort_folder_number[61] sort_folder_number = [x for x in range(0,peopleNumber)] # makedir 61 folders ''' demo功能說(shuō)明: 在folderPath處新建60個(gè)文件夾, 圖片存儲(chǔ)在path處 給每個(gè)文件夾分配150張圖片(將9000張圖片平均分配到60個(gè)文件夾) Tips: 1: os.path.join(path1,path2...) this function is used to combine the path,it returns a path which is 'path1/path2...' 2: os.makedirs(path) this function is used to make a directory(new folder) in the path param 3: shutil.move(oldPath,newPath) this function is used to move file from param1 to param 2 4: os.path.exists(path) this function is used to check the filePath(param1) whether exists ''' for number in sort_folder_number: new_folder_path = os.path.join(folderPath,'%s'%number)#new_folder_path is ‘folderPath\number' if not os.path.exists(new_folder_path): os.makedirs(new_folder_path) print("new a floder named "+str(number)+'at the path of '+ new_folder_path) #give the img list file_list = os.listdir(path) '''define the first foloderNumber''' folderNumber = 1 print('there are '+str(len(file_list))+' files at the path of '+path) for i in range(0,len(file_list)): old_file_path = os.path.join(path,str(i)+'.jpg') if os.path.isdir(old_file_path): '''if the path is a folder,program will pass it''' print('img does not exist ,path=' + old_file_path+' it is a dir' ) pass elif not os.path.exists(old_file_path): '''if the path does not exist,program will pass it''' print('img does not exist ,path='+old_file_path) pass else: '''define the number,it decides how many imgs each people process''' number = 150 #int(len(file_list)/peopleNumber) if(i%number ==0): folderNumber +=1 new_file_path = os.path.join(folderPath,'%s'%(folderNumber)) if not os.path.exists(new_file_path): print('not exist path:'+new_file_path) break shutil.move(old_file_path,new_file_path) print('success move file from '+ old_file_path +' to '+new_file_path)
以上這篇python實(shí)現(xiàn)將多個(gè)文件分配到多個(gè)文件夾的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python簡(jiǎn)單實(shí)現(xiàn)查找一個(gè)字符串中最長(zhǎng)不重復(fù)子串的方法
這篇文章主要介紹了Python簡(jiǎn)單實(shí)現(xiàn)查找一個(gè)字符串中最長(zhǎng)不重復(fù)子串的方法,涉及Python針對(duì)字符串的簡(jiǎn)單遍歷、運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2018-03-03詳解centos7+django+python3+mysql+阿里云部署項(xiàng)目全流程
這篇文章主要介紹了詳解centos7+django+python3+mysql+阿里云部署項(xiàng)目全流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Python?JMESPath庫(kù)輕松操作JSON進(jìn)行數(shù)據(jù)查詢(xún)方法實(shí)例
這篇文章主要為大家介紹了Python?JMESPath庫(kù)輕松操作JSON方法實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01為python爬蟲(chóng)docker鏡像添加nodejs環(huán)境實(shí)現(xiàn)方法
這篇文章主要為大家介紹了為python爬蟲(chóng)docker鏡像添加nodejs環(huán)境實(shí)現(xiàn)方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09python基于moviepy實(shí)現(xiàn)音視頻剪輯
為了方便學(xué)習(xí)和復(fù)習(xí),需要把一些長(zhǎng)音視頻剪成一些小片段。本文介紹如何用Python實(shí)現(xiàn),把音視頻中的某一些片段剪出來(lái)。2021-06-06