Copying contents of one file to a different file

suggest change
with open(input_file, 'r') as in_file, open(output_file, 'w') as out_file:
    for line in in_file:
        out_file.write(line)

This copies text and may translate line endings. For an exact byte copy, open both files in binary mode ('rb' and 'wb').

Using the shutil module copies the bytes as they are:

import shutil
shutil.copyfile(src, dst)

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents