# Subprocess.cal issue - FileNotFoundError: \[WinError 2\]

**URL:** <https://discuss.python.org/t/subprocess-cal-issue-filenotfounderror-winerror-2/25813>\
**Category:** Python Help\
**Created:** [April 14, 2023, 3:15pm UTC](https://discuss.python.org/t/subprocess-cal-issue-filenotfounderror-winerror-2/25813 "2023-04-14T15:15:02Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Susja](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/susja/32/4697_2.png) [@Susja](https://discuss.python.org/u/Susja)\
**Post date:** [April 14, 2023, 3:15pm UTC](https://discuss.python.org/t/subprocess-cal-issue-filenotfounderror-winerror-2/25813/1 "2023-04-14T15:15:03Z")

</div>

## I was using the script successfully when I need to check if some PC is on-line. I used command ‘ping’ for subprocess.call This time I need to check account of User to check if it’s active. Command that I run is: net user /domain USER It works fine from command line but failed in a script. Could someone help me with it? Thanks

```python
#from subprocess import call

import subprocess
import time
import winsound
import datetime

x = subprocess.call(["net user /domain USER|grep -i active"])

# Clear 25 lines of screen for convinience
def clear():
    print('\n' * 25)

clear()

while True:
    if x == 0:
        print (" ******************** \n")
        print ("\n")
        print ("OK")
        print (datetime.datetime.now().strftime('%Y-%m-%d %H:%S'))
        winsound.Beep(500,5000)
        exit()
    else:
        time.sleep(30)
        print ("Wait")
        x = subprocess.call(["net user /domain USER|grep -i active"])
        clear()

```

–  
I see this error:

```python
python WhileLoop_account.py
Traceback (most recent call last):
  File "U:\Scripts\WhileLoop_account.py", line 10, in <module>
    x = subprocess.call(["net user /domain USER|grep -i active"])
  File "C:\Python310\lib\subprocess.py", line 345, in call
    with Popen(*popenargs, **kwargs) as p:
  File "C:\Python310\lib\subprocess.py", line 966, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Python310\lib\subprocess.py", line 1435, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified

```

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [April 14, 2023, 3:59pm UTC](https://discuss.python.org/t/subprocess-cal-issue-filenotfounderror-winerror-2/25813/2 "2023-04-14T15:59:24Z")

</div>

Check the docs for the shell=True option. So that you can pass your command string.

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [April 14, 2023, 4:07pm UTC](https://discuss.python.org/t/subprocess-cal-issue-filenotfounderror-winerror-2/25813/3 "2023-04-14T16:07:04Z")

</div>

Reference on Stack Overflow:

> <https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess>

---

<div class="post-metadata">

**Author:** ![Susja](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/susja/32/4697_2.png) [@Susja](https://discuss.python.org/u/Susja)\
**Post date:** [April 14, 2023, 5:51pm UTC](https://discuss.python.org/t/subprocess-cal-issue-filenotfounderror-winerror-2/25813/4 "2023-04-14T17:51:10Z")

</div>

> [@Susja](#):
>
> `subprocess`

Folks, Thanks so much … providing this parameter fixed the issue.  
One more (minor) thing I hit with my script. I want to have the option to comment/uncomment account that I am interested  
I am using ‘real’ account like this:

```auto
Account = "vhabhsabjoe"
#Account= "vhabhsabnick"

x = subprocess.call('net user /domain Account', shell=True)

```

but it failed to pass Account to sub process. Sounds like my syntax is wrong …  
Here’s the error:

```auto
python WhileLoop_account.py
The request will be processed at a domain controller for domain v01.med.va.gov.

The user name could not be found.

```

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [April 14, 2023, 6:34pm UTC](https://discuss.python.org/t/subprocess-cal-issue-filenotfounderror-winerror-2/25813/5 "2023-04-14T18:34:40Z")

</div>

> [@Susja](#):
>
> but it failed to pass Account to sub process. Sounds like my syntax is wrong

Just having `Account` in the string means the actual letters `Account`, not that variable - just like how you _don’t_ get an error because there isn’t a `net` variable or a `user` variable or a `domain` variable in the program.

A simple command like what you’ve shown could be done better using separate arguments, as shown in the links I provided before. Thus, `subprocess.call(['net', 'user', '/domain', Account])`. (Notice that `Account` has no quotes because we want to put that variable into the list, but the others should be actual strings.)

However, to learn the general technique for creating the string, please see:

> <https://stackoverflow.com/questions/2960772/how-do-i-put-a-variable-s-value-inside-a-string-interpolate-it-into-the-string>

---

<div class="post-metadata">

**Author:** ![Susja](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/susja/32/4697_2.png) [@Susja](https://discuss.python.org/u/Susja)\
**Post date:** [April 14, 2023, 6:48pm UTC](https://discuss.python.org/t/subprocess-cal-issue-filenotfounderror-winerror-2/25813/6 "2023-04-14T18:48:46Z")

</div>

Thanks so much!  
I’m all set now

---

<div class="post-metadata">

**Author:** ![eryksun](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/eryksun/32/697_2.png) [@eryksun](https://discuss.python.org/u/eryksun)\
**Post date:** [April 14, 2023, 11:15pm UTC](https://discuss.python.org/t/subprocess-cal-issue-filenotfounderror-winerror-2/25813/7 "2023-04-14T23:15:37Z")

</div>

> [@kknechtel](#):
>
> A simple command like what you’ve shown could be done better using separate arguments, as shown in the links I provided before. Thus, `subprocess.call(['net', 'user', '/domain', Account])`.

Originally `shell=True` was needed because the output was piped to grep via `"net user /domain USER|grep -i active"`. If you’re not piping to grep, you could also use `subprocess.call(f'net user /domain {Account}')`.

If an argument list is passed, such as `['net', 'user', '/domain', Account]`, then on Windows `subprocess.list2cmdline()` is internally called by `subprocess.Popen` in order to convert the list into a command-line string, according to the rules used by WinAPI `CommandLineToArgvW()` and the C runtime’s `argv` parsing. However, programs are free to use their own command-line parsing rules (e.g. as Cygwin programs do), in which case passing a command-line string may be the only viable solution.

---

<div class="post-metadata">

**Author:** ![Susja](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/susja/32/4697_2.png) [@Susja](https://discuss.python.org/u/Susja)\
**Post date:** [April 17, 2023, 1:27pm UTC](https://discuss.python.org/t/subprocess-cal-issue-filenotfounderror-winerror-2/25813/8 "2023-04-17T13:27:59Z")

</div>

Folks,  
Thanks for suggestions.  
Now my program is working but I hit another … while minor but still related issue.  
I am using bash from MobaXterm app.  
My file is located here: /cygdrive/u/Scripts/WhileLoop\_account.py  
When I run it from the directory /cygdrive/u/Scripts it works fine  
When I run it from other directory it failed with this error:

python /cygdrive/u/Scripts/WhileLoop\_account.py  
C:\Python310\python.exe: can’t open file ‘U:\cygdrive\u\Scripts\WhileLoop\_account.py’: [Errno 2] No such file or directory

When I run it and provide another path it works fine.  
python u:/Scripts/WhileLoop\_account.py  
Question: how I could fix it?

---

<div class="post-metadata">

**Author:** ![eryksun](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/eryksun/32/697_2.png) [@eryksun](https://discuss.python.org/u/eryksun)\
**Post date:** [April 17, 2023, 2:54pm UTC](https://discuss.python.org/t/subprocess-cal-issue-filenotfounderror-winerror-2/25813/9 "2023-04-17T14:54:31Z")

</div>

Windows Python only supports Windows drive-letter paths, UNC paths, and relative paths. In Windows, the path “/cygdrive/u/Scripts/WhileLoop\_account.py” is a relative path that refers to the “/cygdrive” directory on the drive or UNC share of the current working directory. For example, if the working directory is “C:\Temp”, then the relative path resolves to “C:\cygdrive\u\Scripts\WhileLoop\_account.py”.

Cygwin applications don’t use drives, but they reserve the “/cygdrive” directory for accessing Windows drives, such as “/cygdrive/u” in place of Windows “U:\”. You can convert to Windows form using the `cygpath` command, e.g.

```plaintext
python "$(cygpath -w /cygdrive/u/Scripts/WhileLoop_account.py)"

```

Or switch to using a Cygwin build of Python instead of Windows Python.

---

<div class="post-metadata">

**Author:** ![Susja](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/susja/32/4697_2.png) [@Susja](https://discuss.python.org/u/Susja)\
**Post date:** [April 17, 2023, 4:34pm UTC](https://discuss.python.org/t/subprocess-cal-issue-filenotfounderror-winerror-2/25813/10 "2023-04-17T16:34:28Z")

</div>

Thanks so much for explanation

---

<div class="post-metadata">

**Author:** ![Susja](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/susja/32/4697_2.png) [@Susja](https://discuss.python.org/u/Susja)\
**Post date:** [April 19, 2023, 6:46pm UTC](https://discuss.python.org/t/subprocess-cal-issue-filenotfounderror-winerror-2/25813/11 "2023-04-19T18:46:39Z")

</div>

> [@Susja](#):
>
> ```auto
> import subprocess
> import time
> import winsound
> import datetime
> 
> x = subprocess.call(["net user /domain USER|grep -i active"])
> 
> # Clear 25 lines of screen for convinience
> def clear():
> print('\n' * 25)
> 
> clear()
> 
> while True:
> if x == 0:
> print (" ******************** \n")
> print ("\n")
> print ("OK")
> print (datetime.datetime.now().strftime('%Y-%m-%d %H:%S'))
> winsound.Beep(500,5000)
> exit()
> else:
> time.sleep(30)
> print ("Wait")
> x = subprocess.call(["net user /domain USER|grep -i active"])
> clear()
> 
> ```

Well … I realized that I missed important thing in my script. The goal of the script is to figure out if account is Active. For that reason I still need to ‘grep’ for this string ‘Account active Yes’  
Hence I modified my original script and put ‘grep’ in subprocess.call but it stopped to work.  
I’ve got this error and it’s looping:  
python WhileLoop\_account-TEST.py  
The system cannot find the path specified.  
\*\*  
Here is my script now:

```auto
import subprocess
import time
import winsound
import datetime

Account = 'Joe'
#Account = 'Jen'

x = subprocess.call(["'net', 'user', '/domain', Account | grep 'Account active Yes'"], shell=True)

# Clear 25 lines of screen for convinience
def clear():
    print('\n' * 25)

clear()

while True:
    if x == 0:
        print (" ******************** \n")
        print ("\n")
        print ("Account " + Account + " is ACTIVE")
        print (datetime.datetime.now().strftime('%Y-%m-%d %H:%S'))
        winsound.Beep(500,5000)
        exit()
    else:
        time.sleep(30)
        print ("Still trying ... WAIT!!")
        x = subprocess.call(["'net', 'user', '/domain', Account | grep 'Account active Yes'"], shell=True)
        clear()

```

Could some help me with the script?  
Thanks

---

<div class="post-metadata">

**Author:** ![eryksun](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/eryksun/32/697_2.png) [@eryksun](https://discuss.python.org/u/eryksun)\
**Post date:** [April 19, 2023, 7:19pm UTC](https://discuss.python.org/t/subprocess-cal-issue-filenotfounderror-winerror-2/25813/12 "2023-04-19T19:19:24Z")

</div>

Always use a command-line string with `shell=True`. For example:

```auto
x = subprocess.call(f'net user {Account} | grep -E "Account active[[:space:]]+Yes"', shell=True)

```

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [April 19, 2023, 7:23pm UTC](https://discuss.python.org/t/subprocess-cal-issue-filenotfounderror-winerror-2/25813/13 "2023-04-19T19:23:46Z")

</div>

Don’t use grep. You have python!

What i do is run commands and get their output.  
Then i use python to look for the information i am after.  
Something like this:

```auto

p = subprocess.run(f'net user {Account}’, capture_output=True)
If ‘Active’ in p.stdout:
       print(‘its is active’)

```

---

<div class="post-metadata">

**Author:** ![eryksun](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/eryksun/32/697_2.png) [@eryksun](https://discuss.python.org/u/eryksun)\
**Post date:** [April 20, 2023, 1:43pm UTC](https://discuss.python.org/t/subprocess-cal-issue-filenotfounderror-winerror-2/25813/14 "2023-04-20T13:43:26Z")

</div>

> [@barry-scott](#):
>
> ```auto
> p = subprocess.run(f'net user {Account}’, capture_output=True)
> If ‘Active’ in p.stdout:
> print(‘its is active’)
> 
> ```

If the user’s preferred UI language is English, then “Active” will always be in the output, qualified by “Yes” or “No”. When I get the time, I’ll implement this for Susja independent of the configured UI language, using PyWin32 and/or ctypes.

---

<div class="post-metadata">

**Author:** ![Susja](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/susja/32/4697_2.png) [@Susja](https://discuss.python.org/u/Susja)\
**Post date:** [April 20, 2023, 2:18pm UTC](https://discuss.python.org/t/subprocess-cal-issue-filenotfounderror-winerror-2/25813/15 "2023-04-20T14:18:02Z")

</div>

Thank you very.  
It looks that now I’m all set

---

<div class="post-metadata">

**Author:** ![Susja](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/susja/32/4697_2.png) [@Susja](https://discuss.python.org/u/Susja)\
**Post date:** [April 20, 2023, 3:04pm UTC](https://discuss.python.org/t/subprocess-cal-issue-filenotfounderror-winerror-2/25813/16 "2023-04-20T15:04:16Z")

</div>

Well in my case I see only two values: Yes or Locked  
Usually automatic unlocks happens in about a half hour.  
Hence my goal is to visually and vocally notified when account gets unlocked.  
Thanks again for your help and the current script serves my goal

(Attachment me.bmp is missing)
