1. 程式人生 > 其它 >python 自動搜尋並複製檔案

python 自動搜尋並複製檔案

通常情況下,windows會自動給後插入的U盤分配一個碟符,這個碟符的字母應該是已有的最後那個碟符的後一位字母

假如已有C,D,E,F盤,那麼新接入的U盤碟符應該是G(如果你的U盤不分割槽的情況下)

那麼,要實現自動複製到U盤,首先要獲取全部碟符,知道哪個是咱們的U盤

def get_disklist():
  disk_list = []
  for c in string.ascii_uppercase:
    disk = c + ':/'
    if os.path.isdir(disk):
      disk_list.append(disk)
  return disk_list

(在我這裡F盤是移動儲存)

現在已經有一個磁碟的列表了,接下來,獲取此列表長度,根據列表索引鎖定要查詢的磁碟

disk_list=get_disklist()#呼叫get_disklist(),獲取磁碟列表
disk_list_len=len(disk_list)
print("磁碟列表總長度為:"+str(disk_list_len))
print(disk_list)

當然要排除最後一個磁碟,這個很容易實現,只需要排除掉disk_list[-1]即可

for disk_list_present_len in range(0,disk_list_len-1):
  path = disk_list[disk_list_present_len]
  print("當前搜尋磁碟為:"+path)

現在就只鎖定C,D盤了

接下來,做一個互動,讓使用者來決定搜尋什麼東西

filename =input("輸入檔名或檔案型別,將會自動搜尋並複製到最後一個碟符的磁碟內:")

建一個列表,用來儲存搜尋結果

result = []

接下來,到了搜尋的環節了:

i = 0
for root, lists, files in os.walk(path):
    for file in files:
        if filename in file:
            i = i + 1#i為第幾個檔案
            file_path = os.path.join(root, file)
            print('%d %s' % (i, file_path))
            result.append(file_path)

現在已經完成了搜尋檔案並輸出路徑的效果,感覺如何

下一步,應該嘗試複製檔案了,當然第一步是確定咱們要複製到的路徑,也就是咱們的U盤

file_dst_path=disk_list[-1]+filename+"\\"

disk_list[-1]是咱們移動儲存的碟符,filename是剛剛搜尋的檔名

也就是說,咱們是以這個檔名命名資料夾名的

再接下來,判斷這個資料夾是否存在,如果沒有,那就建立一個

judge_file_dst_path=os.path.exists(file_dst_path)
if not judge_file_dst_path:
    os.makedirs(file_dst_path)
    print("已在"+disk_list[-1]+"下建立同文件名的資料夾")

再然後,就到了複製的環節了,這裡使用的是shutil的copy2方法

shutil.copy2(file_src_path,file_dst_path)

到此,所有核心功能已經完成,下面是完整的程式碼

import os,string,shutil


def get_disklist():
  disk_list = []
  for c in string.ascii_uppercase:
    disk = c + ":\\"
    if os.path.isdir(disk):
      disk_list.append(disk)

  return disk_list


disk_list=get_disklist()#呼叫get_disklist(),獲取磁碟列表


disk_list_len=len(disk_list)
print("磁碟列表總長度為:"+str(disk_list_len))
print(disk_list)


filename =input("輸入檔名或檔案型別,將會自動搜尋並複製到最後一個碟符的磁碟內:")
result = []


def auto_search_and_copy():
  for disk_list_present_len in range(0,disk_list_len-1):
    path = disk_list[disk_list_present_len]
    print("當前搜尋磁碟為:"+path)
    
    i = 0
    for root, lists, files in os.walk(path):
        for file in files:
            if filename in file:
                i = i + 1#i為第幾個檔案
                file_path = os.path.join(root, file)
                
                print('%d %s' % (i, file_path))
                result.append(file_path)

                file_src_path=file_path
                file_dst_path=disk_list[-1]+filename+"\\"#拼接,獲取搜尋的檔名並以此在目標磁碟根目錄下建資料夾

                judge_file_dst_path=os.path.exists(file_dst_path)

                #判斷是否有此資料夾
                if not judge_file_dst_path:
                    os.makedirs(file_dst_path)
                    print("已在"+disk_list[-1]+"下建立同文件名的資料夾")

                shutil.copy2(file_src_path,file_dst_path)#複製檔案到目標目錄
                
    
    disk_list_present_len=disk_list_present_len +1

if __name__== '__main__':
  auto_search_and_copy()

如果希望在其他沒有python環節的機器上執行,請使用pyinstaller -F +檔名來打包成可執行exe檔案


更新一下,發現出現同名檔案會被覆蓋現象
將下面程式碼新增到shutil.copy2(file_src_path,file_dst_path)下面即可

                print("複製完成")

                old_file_name=file_dst_path+file
                new_file_name=file_dst_path+str(i)+" "+file
                os.rename(old_file_name,new_file_name)

或者...

import os,string,shutil


def get_disklist():
  disk_list = []
  for c in string.ascii_uppercase:
    disk = c + ":\\"
    if os.path.isdir(disk):
      disk_list.append(disk)

  return disk_list


disk_list=get_disklist()#呼叫get_disklist(),獲取磁碟列表


disk_list_len=len(disk_list)
print("磁碟列表總長度為:"+str(disk_list_len))
print(disk_list)


filename =input("輸入檔名或檔案型別,將會自動搜尋並複製到最後一個碟符的磁碟內:")
result = []


def auto_search_and_copy():
  for disk_list_present_len in range(0,disk_list_len-1):
    path = disk_list[disk_list_present_len]
    print("當前搜尋磁碟為:"+path)
    
    i = 0
    for root, lists, files in os.walk(path):
        for file in files:
            if filename in file:
                i = i + 1#i為第幾個檔案
                file_path = os.path.join(root, file)
                
                print('%d %s' % (i, file_path))
                result.append(file_path)

                file_src_path=file_path
                file_dst_path=disk_list[-1]+filename+"\\"#拼接,獲取搜尋的檔名並以此在目標磁碟根目錄下建資料夾
                judge_file_dst_path=os.path.exists(file_dst_path)

                #判斷是否有此資料夾
                if not judge_file_dst_path:
                    os.makedirs(file_dst_path)
                    print("已在"+disk_list[-1]+"下建立同文件名的資料夾")
                    
                shutil.copy2(file_src_path,file_dst_path)#複製檔案到目標目錄
                print("複製完成")

                old_file_name=file_dst_path+file
                new_file_name=file_dst_path+str(i)+" "+file
                os.rename(old_file_name,new_file_name)
                
                
    
    disk_list_present_len=disk_list_present_len +1

if __name__== '__main__':
  auto_search_and_copy()


本文出自於 https://www.cnblogs.com/tharsis/ 轉載請註明出處。