#文件的复制 ''' def copy(): source = input("请输入文件位置:") target = input("请输入要复制文件的位置:") old_file = open(source, "br") new_file = open(target, "bw") content = old_file.read() new_file.write(content) old_file.close() new_file.close() copy() ''' ''' #复制传递大文件 def copy(): source = input("请输入文件名:") target = input("请输入储存位置:") old_file = open(source, "br")f: new_file = open(target, "bw") while True: content = old_file.read(1024 * 1024) if content: #if content content不为空 开始复制,为空结束 new_file.write(content) else: print("文件复制完成!") break old_file.close() new_file.close() copy() ''' #加备份 ''' def back_file(): file_name = input("请输入与您要备份的文件名称:") # new_name = input("亲输入备份名称:") # 获取文件的后缀名称 sep = file_name.rindex("/") dir = file_name[:sep + 1] name = file_name[sep + 1:] new_file_name = dir + "[备份]" + name old_file = open(file_name, "br") new_file = open(new_file_name, "bw") with open(file_name, "br") as f: while True: content = old_file.read(1024 * 1024) if content: # if content content不为空 开始复制,为空结束 new_file_name.write(content) else: print("文件复制完成!") break old_file.close() new_file.close() #程序入口 back_file() ''' #查询指定目录下的搜有文件 """ import os def list_file(path): child_file = os.listdir(path) for file in child_file: if os.path.isfile(path + "/" + file): print("这是一个文件:%s" % file) else: print(" %s" % (path + "/" + file)) list_file(path + "/" + file) list_file("F:\代码\.metadata") """ #指定目录下搜索文件 import os import sys def search(path, word): for filename in os.listdir(path): fp = os.path.join(path, filename) if os.path.isfile(fp) and word in filename: print(fp) elif os.path.isdir(fp): search(fp, word) search("F:\软件测试","14云计算")#前为搜索的位置,后为要搜索的文件的部分信息
Python文件复制,备份,搜索
最新推荐文章于 2022-11-24 16:03:17 发布