Inside of 'Folder X' I would like to create a copy Invoice.xlsx which is already inside of Folder X and create a copy of it with a new name also inside of Folder X.
How is this done with Python?
Inside of 'Folder X' I would like to create a copy Invoice.xlsx which is already inside of Folder X and create a copy of it with a new name also inside of Folder X.
How is this done with Python?
Please use the Stack Overflow search feature. This question has been answered already.
See How do I copy a file in python? for example.
from shutil import copyfile
copyfile("/path/to/folder X/Invoice.xslx", "/path/to/folder X/Invoice-Copy.xslx")
One approach would be with subprocess
, which lets you run shell commands:
import subprocess
subprocess.run(['cp', 'Invoice.xlsx', 'Invoice_2.xlsx'])
# read file
with open('Folder X/Invoice.xlsx', 'rb') as f:
file = f.read()
# write file
with open('Folder X/Invoice_Copy.xlsx', 'wb') as f:
f.write(file)