shutil
has many methods you can use. One of which is:
from shutil import copyfile copyfile(src, dst) # 2nd option copy(src, dst) # dst can be a folder; use copy2() to preserve timestamp
- Copy the contents of the file named
src
to a file nameddst
. Bothsrc
anddst
need to be the entire filename of the files, including path. - The destination location must be writable; otherwise, an
IOError
exception will be raised. - If
dst
already exists, it will be replaced. - Special files such as character or block devices and pipes cannot be copied with this function.
- With
copy
,src
anddst
are path names given asstr
s.
Another shutil
method to look at is shutil.copy2()
. It’s similar but preserves more metadata (e.g. time stamps).
If you use os.path
operations, use copy
rather than copyfile
. copyfile
will only accept strings.
Related Posts:
- How do I copy a file in Python?
- How do I copy a file in Python?
- How do I copy a file in Python?
- How to open a file using the open with statement
- Using “with open() as file” method, how to write more than once? [duplicate]
- How to read a file line-by-line into a list?
- TypeError: a bytes-like object is required, not ‘str’ when writing to a file in Python3
- Confused by python file mode “w+”
- List changes unexpectedly after assignment. Why is this and how can I prevent it?
- File open and close in python
- Do I understand os.walk right?
- FileNotFoundError: [Errno 2] No such file or directory [duplicate]
- How to move a file in Python?
- Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory
- FileNotFoundError: [Errno 2] No such file or directory
- How to move a file in Python?
- How do you append to a file?
- Writing a list to a file with Python
- Why do I get “Pickle – EOFError: Ran out of input” reading an empty file?
- How do I check whether a file exists without exceptions?
- How to copy a file from one directory to another using PHP?
- Call a function from another file?
- Writing a list to a file with Python
- How can I check file size in Python?
- Python list directory, subdirectory, and files
- Where does this come from: -*- coding: utf-8 -*-
- Open file in a relative location in Python
- Why am I getting a FileNotFoundError?
- How to deep copy a list?
- Python: Write array values into file
- Importing variables from another file?
- Flask raises TemplateNotFound error even though template file exists
- importing external “.txt” file in python
- Search and replace a line in a file in Python
- How to save a dictionary to a file?
- How to open a file for both reading and writing?
- How do you read a file into a list in Python?
- SSL error : routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
- What is the perfect counterpart in Python for “while not EOF”
- .write not working in Python
- Creating files and directories via Python
- How to convert a file into a dictionary?
- How to delete a specific line in a file?
- Python read in string from file and split it into values
- How often does python flush to a file?
- How to open every file in a folder
- TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper
- Loading and parsing a JSON file with multiple JSON objects
- How to find out whether a file is at its `eof`?
- Python copy files to a new directory and rename if file name already exists
- Python IOError: File not open for reading
- How to use “raise” keyword in Python [duplicate]
- How to resolve TypeError: can only concatenate str (not “int”) to str [duplicate]
- Permanently add a directory to PYTHONPATH?
- Finding median of list in Python
- How to solve UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xff in position 0: invalid start byte in python
- ImportError: numpy.core.multiarray failed to import
- Index Error: list index out of range (Python) [duplicate]
- AttributeError: module ‘matplotlib’ has no attribute ‘plot’
- NameError: global name ‘xrange’ is not defined in Python 3
- What does “SyntaxError: Missing parentheses in call to ‘print'” mean in Python?
- No acceptable C compiler found in $PATH when installing python
- How to remove \xa0 from string in Python?
- Running Python from Atom
- What do the symbol “=” and “==” mean in python?
- What is the purpose of “pip install –user …”?
- How to upgrade all Python packages with pip
- SyntaxError: cannot assign to operator
- Using a comparator function to sort
- What is the meaning of single and double underscore before an object name?
- What is the difference between np.mean and tf.reduce_mean?
- How do I combine two dataframes?
- How can I get list of values from dict?
- How do I append one string to another in Python?
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- How to sort a list of objects based on an attribute of the objects?
- Defining lists as global variables in Python
- How to stop Python closing immediately when executed in Microsoft Windows
- Error: “dictionary update sequence element #0 has length 1; 2 is required” on Django 1.4
- module has no attribute
- How to print number with commas as thousands separators?
- double for loops in python
- Convert hex string to int in Python
- Function to close the window in Tkinter
- Is there a way to specify which pytest tests to run from a file?
- How to remove parentheses from string [duplicate]
- BeautifulSoup and lxml.html – what to prefer?
- How to clear up screen in pygame?
- Efficiently sorting a numpy array in descending order?
- python-How to solve KeyError: 2?
- Move column by name to front of table in pandas
- How to search through dictionaries?
- Element-wise addition of 2 lists?
- What do I use for a max-heap implementation in Python?
- Is there a math nCr function in python? [duplicate]
- ImportError: No module named Cython.Distutils
- Python Brute Force algorithm
- What’s the correct way to convert bytes to a hex string in Python 3?
- How can I install a .egg Python package on Windows (attempt using easy_install not working)
- When to use %r instead of %s in Python? [duplicate]