3

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.

Nic
  • 6,211
  • 10
  • 46
  • 69
Towelie
  • 197
  • 1
  • 3
  • 11

2 Answers2

9

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)
Philip I
  • 294
  • 1
  • 5
0

use pyperclip. It's crossplatform.

strangeqargo
  • 1,276
  • 1
  • 15
  • 23