0

code:-

import subprocess
print(subprocess.run(['date']))

when run it:-

Traceback (most recent call last):

File "D:\PyProjects\selenium\0experiment.py", line 8, in <module>
print(subprocess.run(['date']))

File "D:\Users\anaconda3\lib\subprocess.py", line 488, in run
with Popen(*popenargs, **kwargs) as process:

File "D:\Users\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 104, in __init__
super(SubprocessPopen, self).__init__(*args, **kwargs)

File "D:\Users\anaconda3\lib\subprocess.py", line 800, in __init__
restore_signals, start_new_session)

File "D:\Users\anaconda3\lib\subprocess.py", line 1207, in _execute_child
startupinfo)

FileNotFoundError: [WinError 2] The system cannot find the file specified

I am completely new to these and was learning these stuffs from online videos...in which tutor's same code ran flawlessly. Here is the video (at 1:40)


I also tried this code:

import subprocess
print(subprocess.run(['date'], shell=True))

But when ran, above code caused the console to run for long period of time and I have to terminate it manually (without printing anything on console). (WHY??)


I googled a lot and read a few somewhat similar type of question having such kind of Error...but not able to help myself: for example this thread.

  • What is this [WinError 2] , what causes this error?

  • FileNotFoundError , what file is this program referring to?

  • Is there any OS/system related prerequisites that needs to be fulfilled before programs using subprocess module can be executed?

  • I'm using windows 10, Spyder IDE(not installed in C drive but in D drive) having latest version of python.


Please explain in laymen terms. Thanks.

2
  • I don't have windows running at the moment to check, but since no one else has jumped in yet... date may be a Windows shell command so wouldn't be available without shell=True. If you just run "date" on Windows, I think it prompts for a new date to be entered. Does this course specify which operating system it uses? Try something known to be an executable (I think "find" is...?). If it works, write a note to coursera about their example. Commented Jun 29, 2020 at 16:26
  • @tdelaney Yes you are correct, when entered date in command prompt in windows it first prints current date and then prompts for a new date to be entered...And in that course Linux OS is being used by the tutor with every task being done in Terminal (bash).... And I'm sorry but I'm not able to understand what do you mean by 'executable'? Commented Jun 30, 2020 at 5:51

2 Answers 2

1

Try running date on a CMD terminal and it prompts you to enter a new date, That is what's causing your subprocess to hang up indefinitely. You're correct about using shell=True for this command. Use of shell expands your environment variables and the available list of file globs(this is what the win32 error means). Since date here is a specific windows command you need the shell. Using shell=True is also platform dependent so you might want to check what commands you're trying to execute.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for explanation....but please tell how to overcome this and if this is the case then how can I execute cmd commands in python program/ use subprocess module's functions without any error? (Any reference/suggestion will be very helpful)....Also I'm not able to undeerstand what do you mean by:- 'Using shell=True is also platform dependent so you might want to check what commands you're trying to execute.
0

I Have used this in SPyder as well and it runs correctly, my only concern (since I am also new on this) is why does the console asks you to enter a new date. but it works anyway...

import subprocess
subprocess.run(["date"], shell=True)

Output (in spanish):

La fecha actual es: jue. 27/07/2023 
Escriba la nueva fecha: (dd-mm-aa)

First line tells you the date, second line asks you to enter new date as explained above.

3 Comments

This might be of interest regarding your concern: gist.github.com/carlessanagustin/…
@CarlosOswaldoOviedoHernndez Previously when I posted this question I was not able to understand because I was new. Coming to "why console asks to enter new date", please run date command in windows cmd and observe output; subprocess module also helps to achieve the same using python code. You may also see the website mentioned by @colidyre which shows cmd commands and their function.
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.