文件的复制
建立原文件的读取管道和目标文件的写管道
'''
原文件:D:\zhouwei.txt
目标文件:C:\zhouwei.txt
with 结合open使用,可以帮助我们自动释放资源
'''
with open('D:\zhouwei.txt','r') as stream:
container=stream.read()
with open('D:\A\zhouwei.txt','w') as wstream:
wstream.write(container)
print('文件复制完成!')
批量复制文件
open()需要定位到文件,不能是一个目录
os模块#operating system os.path: os.path.dirname(file)#获取当前文件的目录(绝对路径) os.path.join(path,’‘)#返回的是拼接后的新路径
import os
with open(r'D:\zhouwei.txt','r') as stream:
container = stream.read()
file=stream.name
filename=file[file.rfind('\\')+1:]
path = os.path.dirname(__file__)
path1 = os.path.join(path,filename)
with open(path1,'w') as wstream:
wstream.write(container)
|