
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How do I copy a binary file in Python?
The shutil module provides functions for copying files, as well as entire folders.
Calling shutil.copy(source, destination) will copy the file at the path source to the folder at the path destination. (Both source and destination are strings.) If destination is a filename, it will be used as the new name of the copied file. This function returns a string of the path of the copied file.
For example
>>> import shutil >>> # Copy the file in same folder with different name >>> shutil.copy('original.txt', 'duplicate.txt') '/home/username/duplicate.txt' >>> shutil.copy('original.txt', 'my_folder/duplicate.txt') '/home/username/my_folder/duplicate.txt'
The same process can be used to copy binary files as well.
- Related Articles
- How do I copy a file in python?
- How do I delete a file in Python?
- How do I create a .pyc file in Python?
- How do I read a .data file in Python?
- How do I wrap a string in a file in Python?
- How do I check whether a file exists using Python?
- How do I read (or write) binary data in Python?
- How do I include a php.ini file in another php.ini file?
- How to open a file in binary mode with Python?
- How can I source a Python file from another Python file?
- How do I loop through a JSON file with multiple keys/sub-keys in Python?
- How to open a binary file in append mode with Python?
- How to Copy Odd Lines of Text File to Another File using Python
- How to write binary data to a file using Python?
- How to copy a file to a remote server in Python using SCP or SSH?

Advertisements