1

Like normally, we can copy files directly to clipboard by simply ctrl + c

But I want to do that using python

path = "..."
def copy_file_to_clipboard() -> None: 
   clipboard.copy_file(path) # this is a false code, but I want to know the code to do so
Ritwik
  • 21
  • 3
  • Does this answer your question? [How copy file to clipboard using python or CL to paste it using STRG+V later on?](https://stackoverflow.com/questions/73234123/how-copy-file-to-clipboard-using-python-or-cl-to-paste-it-using-strgv-later-on) – kaliiiiiiiii Jan 27 '23 at 14:22
  • Windows, Linux, both? Other? – wwii Jan 27 '23 at 14:52

1 Answers1

0

here is a solution tested in python 3.9 (Notebook jupyter) :

import pyperclip as pc
def copy_to_clipboard(file_name):
    with open(file_name, 'r') as f:
        text = f.read()
        pc.copy(text)
        print("File successfully copied to clipboard!")
copy_to_clipboard('file.txt')
aelhou
  • 1
  • 3