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功能說明:
在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安裝和pycharm環(huán)境搭建設(shè)置方法
這篇文章主要介紹了python安裝和pycharm環(huán)境搭建和設(shè)置方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下 ,2020-05-05
python使用PIL和matplotlib獲取圖片像素點(diǎn)并合并解析
這篇文章主要介紹了python使用PIL和matplotlib獲取圖片像素點(diǎn)并合并解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
python之pygame模塊實(shí)現(xiàn)飛機(jī)大戰(zhàn)完整代碼
這篇文章主要為大家詳細(xì)介紹了python之pygame模塊實(shí)現(xiàn)飛機(jī)大戰(zhàn)完整代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
Python實(shí)現(xiàn)從訂閱源下載圖片的方法
這篇文章主要介紹了Python實(shí)現(xiàn)從訂閱源下載圖片的方法,涉及Python采集的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
Python自動(dòng)化測(cè)試之異常處理機(jī)制實(shí)例詳解
為了保持自動(dòng)化測(cè)試用例的健壯性,異常的捕獲及處理,日志的記錄對(duì)掌握自動(dòng)化測(cè)試執(zhí)行情況尤為重要,下面這篇文章主要給大家介紹了關(guān)于Python自動(dòng)化測(cè)試之異常處理機(jī)制的相關(guān)資料,需要的朋友可以參考下2022-06-06
如何利用Anaconda配置簡(jiǎn)單的Python環(huán)境
這篇文章主要為大家詳細(xì)介紹了如何利用Anaconda配置簡(jiǎn)單的Python環(huán)境,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
python使用any判斷一個(gè)對(duì)象是否為空的方法
這篇文章主要介紹了python使用any判斷一個(gè)對(duì)象是否為空的方法,并給出了改進(jìn)的方法供大家對(duì)比參考,具有一定的借鑒價(jià)值,需要的朋友可以參考下2014-11-11
python簡(jiǎn)單實(shí)現(xiàn)基于SSL的IRC bot實(shí)例
這篇文章主要介紹了python簡(jiǎn)單實(shí)現(xiàn)基于SSL的IRC bot,實(shí)例分析了IRC機(jī)器人的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-06-06

