Python: use copy file command - how it works
This Python guide shows how you can use the copy file command in Python to copy a file. Using a simple example, you can easily do it yourself.
Use copy file command in Python
With the copy file command in Python, files can be easily copied:
- Add the "from shutil import copyfile" command (without quotation marks) to the beginning of your code so that you can use the command afterwards.
- The command syntax is "copyfile (src, dst)". So just insert the values you need for your program.
- An example of this would be: "copyfile ('/ src / dir / file.txt', '/dst/dir/file_with_new_name.txt')".
- As you can see, you can not only copy your file, but also change the name of the file in the target directory.
- Tip: You can also use "/ dst / dir /" instead of "/dst/dir/file_with_new_name.txt" in the command. This copies the file with the original file name and stores it in the target directory.
![](http://hackzl.com/img/internet/8679/python-copy-file-befehl-verwenden-gehts.webp)
Python: Copy file - other options
To copy a file in Python, we introduced the shutil method in the previous paragraph. However, you can also copy a file without the shutil module:
- First of all you have to add the command "import os" to the top of your code.
- Under Windows you can now copy a file with the command "os.system ('copy D: /test.txt F: /')".
- With Linux you have to use the command "os.system ('cp /home/user/test.txt / tmp /')".
- In both cases, a copy command is executed. You can also write other commands between the brackets that are then executed in the terminal or in CMD.
![](http://hackzl.com/img/internet/8679/python-copy-file-befehl-verwenden-gehts-2.webp)
More tips about Python in our CHIP guides
Tip: You can find further programming tutorials on our Python programming language topic page. For example, we will show you how to create and use arrays in Python.