1

Why does Example one copy hello world to my clip board but not Example two?

# Example one
subprocess.check_output(["echo", "hello world", "|", "clip"], shell=True, stderr=subprocess.STDOUT)

# Example two
subprocess.check_output(["echo", "hello \n world", "|", "clip"], shell=True, stderr=subprocess.STDOUT)

Another issues is that Example one copies hello world with quotes around it like so:

"hello world"

So, how do I copy text with multiple lines into the clipboard without double-quotes?

3
  • I don't know why you'd want to use the cmd shell for this; that's a headache. You can simply use p = Popen('clip.exe', stdin=PIPE, stdout=PIPE, universal_newlines=True); p.communicate('hello\nworld'); p.wait().
    – Eryk Sun
    Commented Apr 28, 2015 at 1:14
  • @eryksun why didn't you answer this?! I've got no love for cmd shell. I must need it to work.
    – Gezim
    Commented Apr 28, 2015 at 1:22
  • I wasn't really answering the question as much as avoiding it. Whenever I think I have a handle on how cmd.exe will parse a command line, I get burned.
    – Eryk Sun
    Commented Apr 28, 2015 at 1:32

2 Answers 2

1

As suggested by @eryksun, this solves the issue:

p = subprocess.Popen('clip.exe', stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True)
p.communicate('hello \n world')
p.wait()
0

I suspect it's because you're using shell=True, refactor your code to not use it.

But I would suggest abandoning this approach alltogether and use pyperclip for the clipboard support. It's cross-platform and freely available.

1
  • Nope, it doesn't work at all. FileNotFoundError: [WinError 2] The system cannot find the file specified
    – Gezim
    Commented Apr 28, 2015 at 15:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.