Reputation: 457
I want to open an existing .py file within a python-script. Then i want to save that file but with a new filename. The result should be 2 identical .py files with different names. Would be great, if somebody could explain me how that works. Thank you
Upvotes: 1
Views: 2812
Reputation: 2254
This might be helpful -
with open(file1, 'r') as f1, open(file2, 'w') as f2:
f2.write(f1.read())
Upvotes: 1
Reputation: 4010
with open("source_file", "rb") as f1:
with open("destination_file", "wb") as f2:
f2.write(f1.read())
The result is two identical files:
$ md5sum source_file
65ebdbfe37cc2d221498be0745c85d37 source_file
$ md5sum destination_file
65ebdbfe37cc2d221498be0745c85d37 destination_file
Upvotes: 4
Reputation: 2162
This should do what you're looking for.
with open('first_file.py', 'r') as input:
output = open('copy_file.py', 'w')
output.write(input.read())
Upvotes: 2