""" 复制文件? 从源文件读取 写入到新文件中 """ # # 打开源文件 # source_f = open("作业基础版.py","rb") # # 打开目标文件 # dst_f = open("作业副本.py","wb") # # #从源文件读取数据 写入到目标文件 # data = source_f.read() # dst_f.write(data) # # source_f.close() # dst_f.close() # 并列打开多个文件 可以用逗号隔开 # with open("作业基础版.py","rb") as source_f,open("作业副本.py","wb") as dst_f: # data = source_f.read() # dst_f.write(data) # with 嵌套语法 # with open("作业基础版.py","rb") as source_f: # with open("作业副本.py","wb") as dst_f: # data = source_f.read() # dst_f.write(data) # with 的嵌套时 注意:不要对同一个文件进行操作 # with open("log.txt","rb") as source_f: # with open("log.txt","wb") as dst_f: # data = source_f.read() # dst_f.write(data) # 让用户指定要复制的文件 # source_path = input("请输入源文件路径:").strip() # dst_path = input("请输入目标文件路径:").strip() # # with open(source_path,"rb") as source_f: # with open(dst_path,"wb") as dst_f: # data = source_f.read() # dst_f.write(data) # 基于CMD的复制工具 要达到的效果在CMD中直接输入要复制的文件路径以及 目标文件路径 一回车就完成复制 # 1.需要获取CMD输入的参数 import sys # 有一个文件叫sys import是导入这个文件 # sys文件里有一个变量名称叫argv #print(sys.argv) # 得到的就是执行解释器时传入的参数,第一个参数默认就是当前执行文件 # 源文件路径 source_path = sys.argv[1] # 目标文件路径 dst_path = sys.argv[2] with open(source_path,"rb") as source_f: with open(dst_path,"wb") as dst_f: data = source_f.read() dst_f.write(data)