{"id":4268,"date":"2021-04-27T19:38:00","date_gmt":"2021-04-27T14:08:00","guid":{"rendered":"https:\/\/python-programs.com\/?p=4268"},"modified":"2021-11-22T18:53:43","modified_gmt":"2021-11-22T13:23:43","slug":"python-how-to-copy-files-from-one-location-to-another-using-shutil-copy","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-copy-files-from-one-location-to-another-using-shutil-copy\/","title":{"rendered":"Python : How to copy files from one location to another using shutil.copy()"},"content":{"rendered":"
In this article, we will discuss how to copy files from one directory to another using shutil.copy().<\/p>\n
We have a function named shutil.copy() provided by python shutil module.<\/p>\n
shutil.<\/span>copy<\/span>(<\/span>src, dst, *, follow_symlinks=<\/span>True<\/span>)<\/span><\/pre>\nIt copies the file pointed by src to the directory pointed by dst.<\/p>\n
Parameters:<\/em><\/p>\n\n- src<\/strong> is the file path.<\/li>\n
- dst<\/strong> can be a directory path or file path.<\/li>\n
- if src is a path of symlinks then,\n
\n- if follow_symlinks<\/strong> is True, it will copy the path.<\/li>\n
- if follow_symlinks<\/strong> is False, then it will create a new dst directory in a symbolic link.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n
It returns the path string of a newly created file.<\/p>\n
Now, we will see what module is required, the first step is to import the module.<\/p>\n
import<\/span> shutil<\/span><\/pre>\nNow, we will use this function to copy the files.<\/p>\n
Copy a file to another directory<\/h2>\nnewPath = shutil.<\/span>copy<\/span>(<\/span>'sample1.txt'<\/span>, <\/span>'\/home\/bahija\/test'<\/span>)<\/span><\/pre>\nThe file ‘sample1.txt’ will be copied to the home directory ‘\/home\/bahija\/test’ and after being copied it will return the path of the newly created file that is,<\/p>\n
\/home\/bahija\/test\/sample1.txt<\/pre>\n
\n- If the file name already exists in the destination directory, then it will be overwritten.<\/li>\n
- If no directory exists with the name test inside the \/home\/bahija then the source file will be copied with the name test.<\/li>\n
- If there is no existence of the source file, then it will give an error that is, FileNotFoundError.<\/em><\/li>\n<\/ul>\n
Copy a File to another directory with a new name<\/h2>\n\nCopy a file with new name\r\n<\/span>newPath = shutil.copy<\/span>(<\/span>'sample1.txt'<\/span>, <\/span>'\/home\/bahijaj\/test\/sample2.txt'<\/span>)<\/span><\/pre>\n<\/div>\nThe new name will be assigned to the ‘sample1.txt’ as ‘sample2.txt’ and the file will be saved to another directory.<\/div>\nFew points to note:<\/div>\n\n- The file will be overwritten if the destination file exists.<\/li>\n
- If the file is not available, then it will give FileNotFoundError.<\/em><\/li>\n<\/ul>\n
Copy symbolic links using shutil.copy()<\/h2>\n
Suppose we are using a symbolic link named link.csv which points towards sample.csv.<\/p>\n
link.csv -> sample.csv<\/pre>\nNow, we will copy the symbolic link using shutil.copy() function.<\/p>\n
shutil.<\/span>copy<\/span>(<\/span>src, dst, *, follow_symlinks=<\/span>True<\/span>)<\/span><\/pre>\nWe can see that the follow_symlinks<\/strong> is True by default. So it will copy the file to the destination directory.<\/p>\nnewPath = shutil.<\/span>copy<\/span>(<\/span>'\/home\/bahijaj\/test\/link.csv'<\/span>, <\/span>'\/home\/bahijaj\/test\/sample2.csv'<\/span>)<\/span><\/pre>\nThe new path will be:<\/p>\n
\/home\/bahijaj\/test\/sample2.csv<\/pre>\nSample2.txt is the actual copy of sample1.txt.<\/p>\n
If\u00a0follow_symlinks<\/strong> will be False,<\/p>\nnewPath = shutil.<\/span>copy<\/span>(<\/span>'\/home\/bahijaj\/test\/link.csv'<\/span>, <\/span>'\/home\/bahijaj\/test\/newlink.csv'<\/span>, follow_symlinks=<\/span>False<\/span>)<\/span><\/pre>\nIt will copy the symbolic link i.e. newlink.csv will be a link pointing to the same target file sample1.csv i.e.
\nnewlink.csv -> sample1.txt.<\/p>\n
If the file does not exist, then it will give an error.<\/p>\n
Complete Code:<\/strong><\/p>\n\nimport<\/span> shutil\r\n<\/span>def main<\/span>()<\/span>:\r\n<\/span> # Copy file to another directory\r\n newPath = shutil.copy<\/span>(<\/span>'sample1.txt'<\/span>, <\/span>'\/home\/bahijaj\/test'<\/span>)\r\n<\/span>print(<\/span>\"Path of copied file : \"<\/span>, newPath<\/span>)\r\n<\/span> #Copy a file with new name\r\n newPath = shutil.copy<\/span>(<\/span>'sample1.txt'<\/span>, <\/span>'\/home\/bahijaj\/test\/sample2.txt'<\/span>)\r\n<\/span>print(<\/span>\"Path of copied file : \"<\/span>, newPath<\/span>)\r\n<\/span> # Copy a symbolic link as a new link\r\n newPath = shutil.copy<\/span>(<\/span>'\/home\/bahijaj\/test\/link.csv'<\/span>, <\/span>'\/home\/bahijaj\/test\/sample2.csv'<\/span>)\r\n<\/span>print(<\/span>\"Path of copied file : \"<\/span>, newPath<\/span>)\r\n<\/span> # Copy target file pointed by symbolic link\r\n newPath = shutil.copy<\/span>(<\/span>'\/home\/bahijaj\/test\/link.csv'<\/span>, <\/span>'\/home\/bahijaj\/test\/newlink.csv'<\/span>, follow_symlinks=<\/span>False<\/span>)\r\n<\/span>print(<\/span>\"Path of copied file : \"<\/span>, newPath<\/span>)\r\n<\/span>if __name__<\/span> == <\/span>'__main__'<\/span>:\r\n<\/span>main()<\/span><\/pre>\n<\/div>\nHope this article was useful for you. Enjoy Reading!<\/p>\n
<\/p>\n","protected":false},"excerpt":{"rendered":"
In this article, we will discuss how to copy files from one directory to another using shutil.copy(). shutil.copy() We have a function named shutil.copy() provided by python shutil module. shutil.copy(src, dst, *, follow_symlinks=True) It copies the file pointed by src to the directory pointed by dst. Parameters: src is the file path. dst can be …<\/p>\n