python基础 文件操作相关的函数 发表于 2023-05-17 更新于 2025-06-10 分类于 Python , 爬虫 , 异步 python基础 文件操作相关的函数: #复制文件:shutil.copyfile("oldfile","newfile") #oldfile和newfile都只能是文件shutil.copy("oldfile","newfile") #oldfile只能是文件夹,newfile可以是文件,也可以是目标目录#复制文件夹:shutil.copytree("olddir","newdir") #olddir和newdir都只能是目录,且newdir必须不存在#重命名文件(目录)os.rename("oldname","newname") #文件或目录都是使用这条命令#移动文件(目录)shutil.move("oldpos","newpos") shutil.move("D:/知乎日报/latest/一张优惠券,换你的通讯录信息,你愿意吗?.pdf", "D:/知乎日报/past/") python 异步 读取、写入 文件方式 import aiofiles# 写入文件async def cpoy_wirte_image(self, path, content): # 异步方式执行with操作,修改为 async with async with aiofiles.open(path, "wb") as fp: await fp.write(content) # 读取文件async def read_img(self, path): async with aiofiles.open(path, "rb") as fp: content = await fp.read() return content