ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python 自动搜索并复制文件

2022-05-18 21:32:43  阅读:259  来源: 互联网

标签:python dst list len 复制 搜索 file path disk


通常情况下,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

image-20220517224406252

(在我这里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盘了

image-20220517225111811

接下来,做一个交互,让用户来决定搜索什么东西

filename =input("输入文件名或文件类型,将会自动搜索并复制到最后一个盘符的磁盘内:")

image-20220517225236441

建一个列表,用来存储搜索结果

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)

image-20220517230035158

现在已经完成了搜索文件并输出路径的效果,感觉如何

下一步,应该尝试复制文件了,当然第一步是确定咱们要复制到的路径,也就是咱们的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()

image
本文出自于 https://www.cnblogs.com/tharsis/ 转载请注明出处。

标签:python,dst,list,len,复制,搜索,file,path,disk
来源: https://www.cnblogs.com/tharsis/p/16282860.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有