介绍
这篇文章主要介绍怎样使用python批量查找文件并复制,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
<强> 强>
<强> 强> <强> 强>
py文件结尾的文件。并存放到列表中。另外,加上一定的异常的处理,提高代码的健壮性
<强> 强>
import 操作系统 import os.path #存储py文件 时间=list_total [] #存储其他类型文件 时间=list_qita [] #文件夹路径 def folder_path(路径):#找到当前文件夹下面的文件和文件夹 list =, os.listdir(路径遍历每个文件和文件夹 for n 拷贝列表: 时间=old_path os.path.join(路径,n) 时间=index n.rfind (“!”) if os.path.isfile (old_path),以及n[指数+ 1:]==皃y": list_total.append (n) elif os.path.isdir (old_path): 时间=mm old_path #递归调用 folder_path(毫米) 其他: list_qita.append (n) #主函数 def main (): 时间=m 输入(“请输入文件夹的路径:“).strip () folder_path (m) print () print (“py文件有:“,结束=啊? 打印(list_total) print () print(“其他文件有:“,结束=啊? 打印(list_qita) print() #入口 main () Python之文件的搜索以及复制 2,完成文件的复制粘贴 要求,模拟windows里的实现。 import 操作系统 import os.path #完成文件路径分割 def file_path (): # C: \ \管理员\桌面\ \ a.txt用户 时间=path_old 输入(“请输入文件的路径:“).strip() #文件名+后缀 时间=path_index path_old.rindex (& # 39; \ \ & # 39;) 时间=path_dir path_old [: path_index] # path_name =, path_old [path_index + 1:] 时间=lists os.listdir (path_dir) 打印(列表)#文件后缀 时间=index path_old.rindex (“!”) 时间=dir path_old(指数): 时间=name path_old[指数:]#文件名 时间=filename path_old [path_index + 1:指数) if len(列表)==1: +=path_new dir “,安康;副本“+,名字 其他: num =, len(列表) while num & lt;, 20: if (filename +“,安康;副本“,+,名字),not 拷贝列表: +=path_new dir “,安康;副本“+,名字 elif (filename +“,安康;副本,,,+,“(“,+,str (num), +,“)“, +,名字),拷贝列表: 时间=n 2 while n & lt;, len(列表): if (filename +“,安康;副本,,,+,“(“,+,str (n), +,“)“, +,名字),拷贝列表: , n +=1 其他: +=path_new dir “,安康;副本,,,+,“(“,+,str (n), +,“)“+,名字 打破 其他: +=path_new dir “,安康;副本,,,+,“(“,+,str (num), +,“)“+,名字 num +=1 打破 copy_and_paste_the_files (path_old path_new) #文件复制 def copy_and_paste_the_files (old_path new_path): 时间=old_file 开放(old_path,“rb") 时间=new_file 开放(new_path,“wb") while 真正的: 时间=content old_file.read (1024 * 1024) if 内容: new_file.write(内容) 其他: print(“文件复制完成! ! !“) 打破 old_file.close () new_file.close() #主程序 def main (): file_path() #程序入口 main ()
<强> 强> <强> 强>
以上是怎样使用python批量查找文件并复制的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!