How to copy content inside of a txt file to clipboard? [duplicate]

家住魔仙堡 提交于 2020-08-05 07:59:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!