问题
I have a txt file on my Desktop: test.txt. I want to open that txt file and copy everything to the clipboard.
How do I do it?
I figured how to open file and read lines:
path = 'C:\Users\Username\Desktop\test.txt'
fo = open(path, 'r').readlines()
But I can't figure out how to get that data into the clipboard.
回答1:
You can try using Pyperclip
import pyperclip
fo = open(path, 'r').read()
pyperclip.copy(fo)
If you're on OS X, you can also try this code:
import os
data = "hello world"
os.system("echo '%s' | pbcopy" % data)
回答2:
use pyperclip. It's crossplatform.
来源:https://stackoverflow.com/questions/36391824/how-to-copy-content-inside-of-a-txt-file-to-clipboard