使用Python 3 shutil复制文件并保持destfile可写吗?

RBV

有没有一种方法可以使用Python 3 Shutil复制只读文件,以便目标文件不接收源文件的只读模式?

我已经成功地使用shutil创建了文件的工作副本:

import os, stat

inputfile = 'inputfile.json'    # A read-only file
outputfile = 'outputfile.json'  # A file I want to keep writeable
os.chmod(outputfile, stat.S_IWRITE)    # If outputfile exists, ensure it's writeable
shutil.copy(inputfile, outputfile)  # Rats! -- shutil included read-only attributes in copy operation

但是shutil还复制了输入文件的只读属性以及文件内容。我不要

显然,在复制操作之后,我可以重复os.chmod命令。而且我知道如何在不使用shutil的情况下创建可写副本但是是否可以使用shutil复制文件的内容而不复制其属性(?)

伊格纳西奥·巴斯克斯(Ignacio Vazquez-Abrams)

随心所欲地打开文件,并用于shutil.copyfileobj()将文件内容仅从一个文件复制到另一个文件。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章