最新消息: 新版网站上线了!!!

Python3批量移动指定文件到指定文件夹方法示例

ÒýÑÔ

ijÈËÐèÇó£ºÒÔijexcelÖÐÐÕÃûÐÅϢΪÃû½¨Á¢Ò»ÏµÁÐÎļþ¼Ð£¬·Ö±ð½«ËĸöÎļþ¼ÐÖÐÓëÈËÃû¶ÔÓ¦µÄÎļþ»ã×ܵ½¸ÃÈËÃû¶ÔÓ¦µÄÎļþ¼ÐÖУ¬¹²½ü200ÈË£¬ÊÖ¹¤´¦Àí·Ñʱ·ÑÁ¦¡£

ÐèÇó·Ö½â£º

´ÓexcelÖÐÌáÈ¡´úºÅindexºÍÐÕÃûÐÅÏ¢name
ÒÔindex_name¸ñʽÅúÁ¿½¨Á¢Îļþ¼Ðnamefolder
·Ö±ð±éÀúËĸöÎļþ¼Ð¼°Æä×ÓÎļþ¼Ð£¬½«ÆäÖС°ÎļþÃûfilenameÖаüº¬ÐÕÃûname¡±µÄÎĵµÒƶ¯µ½¸ÃÈ˶ÔÓ¦µÄн¨µÄÎļþ¼ÐnamefolderÖÐ

excelÄÚÈÝ£º 


´¦ÀíÇ°£º 

 

´¦Àíºó£º 


¹¦ÄÜʵÏֱȽÏÈÝÒ×£¬È¨µ±¼Ç¼¡£

»·¾³£ºPython3.6+macOS10.12.5

´úÂë

# -*- coding: utf-8 -*-

import os
import shutil
import xlrd
import string

### ´´½¨¶à²ãĿ¼
def mkdirs(path):
 # È¥³ýÊ×λ¿Õ¸ñ
 path = path.strip()
 # È¥³ýβ²¿ \ ·ûºÅ
 path = path.rstrip("\\")

 # ÅжÏ·¾¶ÊÇ·ñ´æÔÚ
 # ´æÔÚ  True
 # ²»´æÔÚ False
 isExists = os.path.exists(path)

 # ÅжϽá¹û
 if not isExists:
  # ´´½¨Ä¿Â¼²Ù×÷º¯Êý
  os.makedirs(path)
  # Èç¹û²»´æÔÚÔò´´½¨Ä¿Â¼
  print(path + ' ´´½¨³É¹¦')
  return True
 else:
  # Èç¹ûĿ¼´æÔÚÔò²»´´½¨£¬²¢ÌáʾĿ¼ÒÑ´æÔÚ
  print(path + ' Ŀ¼ÒÑ´æÔÚ')
  return False

def search_file(path, filename, newpath):
 queue = []
 queue.append(path);
 while len(queue) > 0:
  tmp = queue.pop(0)
  if (os.path.isdir(tmp)):#Èç¹û¸Ã·¾¶ÊÇÎļþ¼Ð
   for item in os.listdir(tmp):#±éÀú¸Ã·¾¶ÖÐÎļþºÍÎļþ¼Ð
    queue.append(os.path.join(tmp, item))#½«ËùµÃ·¾¶¼ÓÈë¶ÓÁÐqueue
  elif (os.path.isfile(tmp)):#Èç¹û¸Ã·¾¶ÊÇÎļþ
   name = os.path.basename(tmp) # »ñÈ¡ÎļþÃû
   dirname = os.path.dirname(tmp) # »ñÈ¡ÎļþĿ¼
   full_path = os.path.join(dirname, name) # ½«ÎļþÃûÓëÎļþĿ¼Á¬½ÓÆðÀ´£¬ÐγÉÍêÕû·¾¶
   des_path = newpath+'/'+path+'_'+name #Ä¿±ê·¾¶£¬½«¸ÃÎļþ¼ÐÐÅÏ¢Ìí¼Ó½ø×îºóµÄÎļþÃûÖÐ
   if filename in name:#Æ¥Åä·ûºÏÌõ¼þµÄÎļþ£¬Ò²¿ÉÓÃif(name.find(filename)!=-1):
    shutil.move(full_path, des_path)#Òƶ¯Îļþµ½Ä¿±ê·¾¶£¨Òƶ¯+ÖØÃüÃû£©

if __name__ == '__main__':
 #´ò¿ªexcelÎļþ
 data=xlrd.open_workbook('Ãûµ¥.xlsx')
 #»ñÈ¡µÚÒ»ÕŹ¤×÷±í£¨Í¨¹ýË÷ÒýµÄ·½Ê½£©
 table=data.sheet_by_index(0)
 #datalistÓÃÀ´´æ·ÅÊý¾Ý
 datalist_UNIQID=[]
 datalist_NAME=[]
 #½«tableÖеÚÒ»ÐеÄÊý¾Ý¶ÁÈ¡²¢Ìí¼Óµ½data_listÖÐ
 datalist_UNIQID.extend(table.col_values(0))
 datalist_NAME.extend(table.col_values(2))
 #foldname = [a+'_'+b for a, b in zip(datalist_UNIQID,datalist_NAME)]
 dir_tjbg='zzz±¨¸æ'
 dir_fsgz='xxxÖ¤Êé'
 dir_hbpx='yyyÖ¤Êé'
 dir_ykfs='xyzÖ¤Êé'
 #´òÓ¡³öµÚÒ»ÐеÄÈ«²¿Êý¾Ý
 for index, name in zip(datalist_UNIQID, datalist_NAME):
  foldername=index+'_'+name.strip()
  #print(foldername)
  mkdirs(foldername)
  search_file(dir_tjbg,name.strip(),foldername)
  search_file(dir_fsgz,name.strip(),foldername)
  search_file(dir_hbpx,name.strip(),foldername)
  search_file(dir_ykfs,name.strip(),foldername)

±¸Íü

1. ͬ²½±éÀúÁ½¸ölist

l1 = [2,2,2,2]
l2 = [3,3,3,3]
prod = [a*b for a, b in zip(l1,l2)]
print prod
# [6, 6, 6, 6]
add = [a+b for a, b in zip(l1,l2)]
print add
# [5, 5, 5, 5]

2. Îļþ¼Ð²Ù×÷

import os
import shutil

os.getcwd() # »ñÈ¡µ±Ç°¹¤×÷Ŀ¼£¬·Ç½Å±¾Ä¿Â¼
os.listdir() # ·µ»ØÖ¸¶¨Ä¿Â¼ÏµÄËùÓÐÎļþºÍĿ¼£¬·ÇµÝ¹é
os.remove() # ɾ³ýÎļþ
os.removedirs() #ɾ³ýĿ¼
os.path.isfile() # ¼ìÑé¸ø³öµÄ·¾¶ÊÇ·ñÊÇÒ»¸öÎļþ
os.path.isdir() # ¼ìÑé¸ø³öµÄ·¾¶ÊÇ·ñÊÇÒ»¸öĿ¼
os.path.isabs() # ÅжÏÊÇ·ñÊǾø¶Ô·¾¶
os.path.exists() # ¼ìÑé¸ø³öµÄ·¾¶ÊÇ·ñÕæʵ´æÔÚ
os.path.split() # ·µ»ØÒ»¸ö·¾¶µÄĿ¼ÃûºÍÎļþÃû
os.path.splitext() # ·ÖÀëÎļþÀ©Õ¹Ãû
os.path.dirname() # »ñÈ¡Îļþ·¾¶Ãû
os.path.basename() # »ñÈ¡Ò»¸ö¾ø¶Ô·¾¶ÏµÄÎļþÃû
os.system() # ÔËÐÐshellÃüÁî
os.rename(old,new) # ÖØÃüÃûÎļþ»òĿ¼
os.makedirs(r"c:\python\test") # ´´½¨¶à¼¶Ä¿Â¼
os.mkdir("test") # ´´½¨µ¥¸öĿ¼
os.exit() # ÖÕÖ¹µ±Ç°½ø³Ì
os.path.getsize(filename) # »ñÈ¡Îļþ´óС
os.mknod("test.txt") # ´´½¨¿ÕÎļþ

shutil.copyfile("oldfile","newfile") # oldfileºÍnewfile¶¼Ö»ÄÜÊÇÎļþ
shutil.copytree("olddir","newdir") # olddirºÍnewdir¶¼Ö»ÄÜÊÇĿ¼£¬ÇÒnewdir±ØÐë²»´æÔÚ
shutil.move("oldpos","newpos") # Òƶ¯Îļþ»òĿ¼
shutil.rmtree("dir") # ɾ³ýĿ¼£¬Óëos.removedirs()Ïàͬ
os.path.join(¡°home¡±, "me", "mywork") # ·¾¶Á¬½Ó

²Î¿¼

×ܽá

ÒÔÉϾÍÊÇÕâƪÎÄÕµÄÈ«²¿ÄÚÈÝÁË£¬Ï£Íû±¾ÎĵÄÄÚÈݶԴó¼ÒµÄѧϰ»òÕß¹¤×÷¾ßÓÐÒ»¶¨µÄ²Î¿¼Ñ§Ï°¼ÛÖµ£¬Ð»Ð»´ó¼Ò¶Ô½Å±¾Ö®¼ÒµÄÖ§³Ö¡£

转载请注明:谷谷点程序 » Python3批量移动指定文件到指定文件夹方法示例