用python批量移動(dòng)文件
更新時(shí)間:2021年01月14日 11:11:54 作者:風(fēng)中狂笑
這篇文章主要介紹了如何用python批量移動(dòng)文件,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
我是用來移動(dòng)圖片的,其他格式的文檔也是可以的,改下后綴列表就可以了
import os,shutil
import datetime
#將文件夾里的圖片全部移動(dòng)到新文件夾中
#revised by Stephen Shen 2020-3-10 09:28:50
def renameFile(dstpath):
fdirname,fbasename=os.path.split(dstpath)
#文件名相同但大小不同
fname,fext=os.path.splitext(fbasename)
nowtime=datetime.datetime.now()
strtime=str(nowtime.year)+str(nowtime.month)+str(nowtime.day)+str(nowtime.hour)+str(nowtime.minute)
newfbasename=fname+'-'+strtime+fext
dstpath=os.path.join(fdirname,newfbasename)
return dstpath
def moveFile(oldpath,newpath):
if os.path.exists(newpath):
newpath=renameFile(newpath)
try:
shutil.move(oldpath,newpath)
print(oldpath+' is moved')
except:
print(oldpath+' is skipped')
inpath=r'K:\fileExtracted\imagesFromDocs'
outpath=r'K:\filesExtracted'
image_ext=['.JPG','.jpg','.png','.PNG','.jpeg','.wdp']
image_outpath=os.path.join(outpath,'image')
doc_ext=['.doc','.docx']
doc_outpath=os.path.join(outpath,'doc')
emf_ext=['.emf']
emf_outpath=os.path.join(image_outpath,'emf')
wmf_ext=['.wmf']
wmf_outpath=os.path.join(image_outpath,'wmf')
if not os.path.exists(outpath):
os.makedirs(outpath)
if not os.path.exists(image_outpath):
os.makedirs(image_outpath)
if not os.path.exists(doc_outpath):
os.makedirs(doc_outpath)
if not os.path.exists(emf_outpath):
os.makedirs(emf_outpath)
if not os.path.exists(wmf_outpath):
os.makedirs(wmf_outpath)
for folder,subfolders,files in os.walk(inpath):
for file in files:
oldpath=os.path.join(folder,file)
if os.path.splitext(file)[-1] in image_ext:
newpath=os.path.join(image_outpath,file)
moveFile(oldpath,newpath)
elif os.path.splitext(file)[-1] in doc_ext:
newpath=os.path.join(doc_outpath,file)
moveFile(oldpath,newpath)
elif os.path.splitext(file)[-1] in emf_ext:
newpath=os.path.join(emf_outpath,file)
moveFile(oldpath,newpath)
elif os.path.splitext(file)[-1] in wmf_ext:
newpath=os.path.join(wmf_outpath,file)
moveFile(oldpath,newpath)
else:
continue
print('done')
然后再刪除空文件夾
import os,shutil
#將文件夾里的空文件夾刪除
#revised by Stephen Shen 2020-3-8 17:50:24
inpath=r'E:\pics-moving\待分類照片'
for folder,subfolders,files in os.walk(inpath):
if not os.listdir(folder):
shutil.rmtree(folder)
# print(folder+' is empyt')
print(folder+' is deleted')
print('done')
以上就是用python批量移動(dòng)文件的詳細(xì)內(nèi)容,更多關(guān)于python批量移動(dòng)文件的資料請關(guān)注腳本之家其它相關(guān)文章!
您可能感興趣的文章:
- python 批量重命名移動(dòng)文件
- 利用python清除移動(dòng)硬盤中的臨時(shí)文件
- python--shutil移動(dòng)文件到另一個(gè)路徑的操作
- Python3批量移動(dòng)指定文件到指定文件夾方法示例
- python腳本之一鍵移動(dòng)自定格式文件方法實(shí)例
- python 移動(dòng)圖片到另外一個(gè)文件夾的實(shí)例
- python實(shí)現(xiàn)指定文件夾下的指定文件移動(dòng)到指定位置
- python 剪切移動(dòng)文件的實(shí)現(xiàn)代碼
- python實(shí)現(xiàn)批量移動(dòng)文件
相關(guān)文章
python實(shí)現(xiàn)字典(dict)和字符串(string)的相互轉(zhuǎn)換方法
這篇文章主要介紹了python實(shí)現(xiàn)字典(dict)和字符串(string)的相互轉(zhuǎn)換方法,涉及Python字典dict的遍歷與字符串轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
django實(shí)現(xiàn)同一個(gè)ip十分鐘內(nèi)只能注冊一次的實(shí)例
下面小編就為大家?guī)硪黄猟jango實(shí)現(xiàn)同一個(gè)ip十分鐘內(nèi)只能注冊一次的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
在Python的Flask框架中驗(yàn)證注冊用戶的Email的方法
這篇文章主要介紹了在Python的Flask框架中驗(yàn)證注冊用戶的Email的方法,包括非常詳細(xì)的測試過程,極力推薦!需要的朋友可以參考下2015-09-09
實(shí)時(shí)獲取Python的print輸出流方法
今天小編就為大家分享一篇實(shí)時(shí)獲取Python的print輸出流方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
VScode查看python f.write()的文件亂碼問題及解決方法
這篇文章主要介紹了VScode查看python f.write()的文件亂碼問題及解決方法,本文通過圖文并茂的形式給大家分享解決方法,需要的朋友可以參考下2023-02-02

