In addition to using the shutil methods above, you can also use pyfile or pyfileobj.
So, if you're pasting the source file into your current working directory, your code should look like this: import shutil If you do, Python raises a syntax exception. Therefore you need to include it if the source and destination files are in different directories.īut if both files are in your current working directory, you don't need to include a follow_symlinks. The follow_symlinks argument is a symbolic link declaration that lets shutil follow an absolute path. For instance, the destination file retains the date you created the source file, its size, and other attributes. The method above preserves the original metadata of the old file in the destination file. py2(sourceFile, destinationFile, follow_symlinks=True) However, to copy the metadata along with the original file, use the py2 method: import shutil In essence, your machine sees the destination file as a new one entirely and gives it new metadata. The py method doesn't copy the metadata of the source file. Related: Command Prompt (CMD) Commands You Must Know However, you can also use the default Python Integrated Development Environment (IDLE) if you're not familiar with running Python via the command line. You can then run it using the command line. Note that you can write your code in any code editor of your choice. py(sourceFile, destinationFile, follow_symlinks=True) SourceFile = "C:/Users/some_directories/my_folder/copy.txt"ĭestinationFile = "C:/Users/some_directories/destination/newFile.txt" It then renames the destination file as newFile.txt: import shutil In essence, shutil takes this format: py(,, follow_symlinks=True)įor example, the code below uses py() to copy a file named copy.txt from a folder called my_folder into another one named destination.
To use it, you only need to supply the full path of the source and destination files. The shutil module is a built-in and powerful Python library for reading and writing files quickly in Python. How to Copy Files Using Python's Built-in Shutil Library Let's see how we can use these various methods to copy files with Python. However, one of the popular ways of copying files with Python is using the shutil library. While you may not need extra modules to copy files with Python, you can still use built-in libraries if you want.įor instance, if you want to use the os method, you need to import it into your code. So, the requirement for doing it varies and depends on the method you decide to use. There are many ways to copy and paste files with Python. Requirements for Copying Files With Python
So, let's find out how to copy files with Python through this post. If you're new to Python, it's a skill you might still need to pick up one way or another. In addition to being fun, it lets you read and write files quickly without getting distracted from your code. Copying and pasting files from one directory to another with Python is a fun way to do this. Do you want to improve your Python skill? Then you can try carrying out some tasks on your computer with Python.