import os # 将文件路径名转换为绝对路径名 # print(os.path.abspath('00-test.txt')) # 拷贝文件 def copy(src, dst): # 同一地址无需拷贝 if os.path.abspath(src) == os.path.abspath(dst): print('地址相同,无需拷贝') return # 判断是否是目录 if os.path.isdir(src): print('源文件是目录文件,无法拷贝') return # 判断目标地址是否是目录 if os.path.isdir(dst): # 提取源文件名 src_name = os.path.basename(src) # 拼接目标文件名 dst = os.path.join(dst, src_name) # 打开源文件 src_fp = open(src, 'r') # 打开目标文件 dst_fp = open(dst, 'w') while True: # 读取指定长度的内容 content = src_fp.read(1024) # 判断是否读完 if len(content) == 0: break # 将内容写入目标文件 dst_fp.write(content) # 关闭源文件 src_fp.close() # 关闭目标文件 dst_fp.close() # 不指定目标文件,会使用原文件名 # copy('00-test.txt', 'test') # 指定目标文件名,使用指定的 # copy('00-test.txt', 'test/3.txt')
python 拷贝一个文件
最新推荐文章于 2024-07-09 21:36:08 发布