2016-05-03 59 views
-1

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

+1

这是因为打开使用'文件与开放('文件1那样简单.py','r')',然后在写入模式下打开另一个'file2.py'并写入'file1'的内容 –

+0

,您可以在这里找到相关信息:https://docs.pytho n.org/2/library/shutil.html – Elisha

+1

只需检查文档或许多python教程之一,了解如何读取和写入文件。你可以搜索“读取写入文件python”或“复制文件python”,并在比你写这个问题更少的时间内得到答案。 – jDo

回答

0

这应该做你想找的。

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

这可能是有益的 -

with open(file1, 'r') as f1, open(file2, 'w') as f2: 
    f2.write(f1.read()) 
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