DEV Community

Discussion on: Python for Bash

Collapse
 
pavi2410 profile image
Pavitra Golchha • Edited

Can you tell me the difference between these codes?

result = subprocess.check_output('ls ./drafts', shell=True)
lines = result.splitlines()
    for line in lines:
        line = str(line, 'utf-8')
lsprocess = sp.run("ls ./_drafts", shell=True)
fileList = lsprocess.stdout.decode('utf-8').strip().split("\n")
Collapse
 
_andy_lu_ profile image
Andy Lu

I caught a bug in my code! Thank you for pointing it out!

In order for Example 2 to run properly, it should read:

lsprocess = sp.run("ls ./_drafts", shell=True, stdout=sp.PIPE)

Running the snippet in the interpreter really quick confirms it. lsprocess.stdout doesn't even exist without the stdout=sp.PIPE part.

Collapse
 
_andy_lu_ profile image
Andy Lu

Nothing as far as I can tell.

Example 1 uses subprocess.check_output(), but Example 2 uses the CompletedProcess class in subprocess.

What difference am I missing here?