脚本中的Python open .py文件:使用新名称保存.py文件

时间:2016-05-03 13:50:04

标签: python

我想在python脚本中打开一个现有的.py文件。然后我想保存该文件但使用新文件名。结果应该是2个具有不同名称的相同.py文件。如果有人可以解释我的工作方式,那会很棒。谢谢

3 个答案:

答案 0 :(得分:0)

这应该做你想要的。

with open('first_file.py', 'r') as input:
    output = open('copy_file.py', 'w')
    output.write(input.read())

答案 1 :(得分:0)

这可能会有所帮助 -

with open(file1, 'r') as f1, open(file2, 'w') as f2:
    f2.write(f1.read())

答案 2 :(得分:0)

with open("source_file", "rb") as f1:
    with open("destination_file", "wb") as f2:
        f2.write(f1.read())

结果是两个相同的文件:

$ md5sum source_file 
65ebdbfe37cc2d221498be0745c85d37  source_file
$ md5sum destination_file 
65ebdbfe37cc2d221498be0745c85d37  destination_file
相关问题