0

So i wanted to capture a whole directory with Subprocess.Popen so i used Stdout=subprocess.PIPE. However when i try to print the directory (which had a lot of files), only half of them showed up.

So what i did was:

import subprocess

data = subprocess.Popen(["dir","C:\\etc\\etc\\etc"],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE)
data = (data.communicate()[0]).decode(encoding='cp1250')
print(data)

And about the half of the files and directories where shown, and like i said maybe it was because of the big amount of files in the folder.

  • 2
    There's no reason this shouldn't work. That said, a *far* more efficient solution would be to use `os.scandir` (or the less powerful but slightly simpler `os.listdir`) rather than delegating to a subprocess. Delegating simple tasks like this to a subprocess is usually a terrible idea; `os.scandir` is using the same underlying API as `dir` uses, but without needing to launch a process that formats it all to text, and pipes it back to the parent, which would have to go to more trouble to parse it. – ShadowRanger Jun 03 '22 at 02:56
  • @ShadowRanger Thank you very much! Yeah now it is looking much better, i just thought that the standart API would look good but now it's looking great. Appreciate your help! – ElismaDM Jun 03 '22 at 03:10

0 Answers0