fork download
  1. import shutil
  2.  
  3. def duplicate_file(src, dst):
  4. try:
  5. shutil.copy2(src, dst) # Copies the file src to the file or directory dst.
  6. print(f"File '{src}' duplicated to '{dst}' successfully.")
  7. except FileNotFoundError:
  8. print(f"Error: '{src}' not found.")
  9. except PermissionError:
  10. print(f"Error: Permission denied to access '{src}' or write to '{dst}'.")
  11.  
  12. # Example usage:
  13. source_file = "example.txt"
  14. destination_file = "example_copy.txt"
  15.  
  16. duplicate_file(source_file, destination_file)
Success #stdin #stdout 0.04s 10256KB
stdin
Standard input is empty
stdout
Error: 'example.txt' not found.