Python Section 9-17 Running External Programs: Subprocess Error

Hello,

I am currently on Section 9-17 (Running External Programs) from the Python course. I copied the code exactly as Mosh had, but mine shows an error, and I cannot seem to figure out how to fix it, or what my problem is. Could anyone help? I have attached an image to show my code as well as my error.


(Also, I am on Windows, I don’t know if that makes a difference)

Thank you!

2 Likes

I think ls -l is a Mac/Unix thing. It’s used to list all the files in the directory. Windows has some other command.

You can try doing something like subprocess.run(["echo", "test"]) and see if python still works fine?

2 Likes

Hmm, it doesn’t seem to be working. I’m not sure if I am doing something wrong. This is what I get when I run it:

Thanks for the help :slight_smile:

Huh it might be because “echo” isn’t a file.
You can try adding shell=True?

subprocess.run(["echo", "test"], shell=True)

1 Like

I tried that, and now when I run it in both the output and the terminal, I get: test. This doesn’t seem to be what Mosh gets, however. :thinking:

Yes that’s okay, at least we know Python is working fine.

The closest you can get to what he has is if you use
subprocess.run(["dir"], shell=True)

It won’t look exactly the same since he is using a mac. :disappointed:

4 Likes

I see. Thank you so much for your help! :grin:

I’m using Mac too, but sometimes Mosh’s code don’t work for me too on VSC. Try yourself a PyCharm Community.

1 Like

Thanks for your help! :laughing: :laughing:
(maybe I should change for mac to learn programming to avoid these weird problem. :roll_eyes: