0

I have a script that writes a bunch of new files to a directory. After every file it writes, I want to print the results of an ls command. The issue is, if I call

import subprocess

p = subprocess.Popen(['ls'], stdout=subprocess.PIPE)
print ''.join(p.stdout.readlines())

Then eventually (after having written many files) there is an "OSError: [Errno 12] cannot allocate memory" thrown by the line

p = subprocess.Popen(['ls'], stdout=subprocess.PIPE)

because subprocess is calling os.fork() internally, which doubles the memory currently in use.

As suggested by another stack overflow (https://stackoverflow.com/a/13329386), we should be able to fix this by initializing a single Popen object at the beginning of the script, then spawning child processes as needed. However, when I do

p = subprocess.Popen(['ls'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
while True:
    p.stdin.write('ls')
    print p.stdout.readlines()

An "IOError: [Errno 32] Broken pipe" is thrown at the first p.stdin.write('ls'), because the process has already finished executing and the input pipe was closed.

Any thoughts on how to achieve this?

Dan
  • 153
  • 8
  • 1
    Why don't you use `subprocess.check_output()`? – nnnmmm Mar 18 '20 at 15:18
  • Why not doing it in Python ? https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory – Jona Mar 18 '20 at 15:20
  • I could try to switch to the os command but I still need this answer for generalizing other similar commands – Dan Mar 18 '20 at 15:23
  • Is `subprocess.run(['watch', 'ls'])` an option? – chepner Mar 18 '20 at 15:25
  • @chepner ideally I'll get to choose when to ls by writing to stdin of the subprocess. Example above was just to show that it doesn't work when I try to write to it – Dan Mar 18 '20 at 15:30
  • That's because you are running `ls`, not a shell that takes the string `ls` as input in order to execute `ls`. – chepner Mar 18 '20 at 15:31
  • Would that fix things? Not really sure what you mean – Dan Mar 18 '20 at 15:39

0 Answers0