0

The code is the following (on a python 2.7 prompt)

import subprocess as sp

a = sp.Popen(['bash', '-c', 'ssh [REDACTED] cat text.txt'],
             stdout=sp.PIPE, stderr=sp.PIPE)    
print(a.communicate()[0])

I would expect this program to print the entire "text.txt" file, however, it is missing the last few lines.

The file text.txt is in a remote server, which I assume is the gist of it. If I retrieve the file and then do subprocess.Popen locally, it works as expected.

Why does it happen and how can I fix it?

note: the result is the same if I use check_output

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • If you run that ssh command locally, do you get the full file, or is that also missing the last few lines? – amiller27 May 15 '16 at 16:46
  • I get the full file. The remote server might be doing something funny and not sending everything through stdout somehow? –  May 15 '16 at 16:52
  • what happens if you run: `print(repr(subprocess.check_output(shlex.split('ssh "cat text.txt"'))))`? – jfs May 16 '16 at 23:07

1 Answers1

0

According to the documentation for communicate, the command shouldn't be used if the data size is very large. Try something like this and see if that solves your problem.

Community
  • 1
  • 1
amiller27
  • 491
  • 5
  • 8
  • it didn't solve it, and the size of it shouldn't be massive (the txt itself is not massive at least) –  May 15 '16 at 17:25
  • What happens if you store `text.txt` locally and then replace your `ssh` command with just `cat text.txt`? – amiller27 May 15 '16 at 17:29
  • @amiller27: [update your question](http://stackoverflow.com/posts/37240933/edit) and specify an approximate size of the file (1K, 100K, 1M, 10M, 1G, etc)? – jfs May 16 '16 at 23:09