0
import os
from subprocess import PIPE,Popen
#os.chdir("..")
cmd=["ls","*.py"]

try:
    p=Popen(cmd,stdout=PIPE,universal_newlines=True,shell=True)
except Exception as e:
    print(f"Exception:\t{e}")
for line in p.stdout:
    print(line,end=" ")
p.stdout.close()
return_code=p.wait()

I am trying to get ls all python files in my server, however whenever I am writing the output which should be all .py files only, I am getting every file there, what am I doing wrong. I tried ls *.py in server terminal and it works fine,however in script not working

Piggydog
  • 21
  • 1
  • 6

1 Answers1

1

You can directly use "ls *.py" as a command

import os
from subprocess import PIPE,Popen
#os.chdir("..")
cmd=["ls *.py"]

try:
    p=Popen(cmd,stdout=PIPE,universal_newlines=True,shell=True)
except Exception as e:
    print(f"Exception:\t{e}")
for line in p.stdout:
    print(line,end=" ")
p.stdout.close()
return_code=p.wait()
Vikas Mulaje
  • 727
  • 6
  • 11