FAQ
Hi,
I have this code:


#!\Python27\python


import subprocess
#subprocess.call(['SchTasks /Create /SC ONCE /TN "My Tasks" /TR "C:/Program
Files/Apache Group/Apache2/htdocs/ccc/run_alert.py" /ST 07:50'], shell=True)
subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py"
/ST 07:50'], shell=True)
With either call, I get this error:
C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py
The system cannot find the path specified.


If I remove the ", shell=True" I get this:
C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py
C:\Program Files\Apache Group\Apache2\htdocs\ccc\cron_alert_activity.py,
line 4, in <module>
subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py"
/ST 07:50'])
File "C:\Python27\lib\subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 679, in __init__ errread,
errwrite)
File "C:\Python27\lib\subprocess.py", line 896, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
The file exists in said directory. I can execute it from the cmd prompt.


So I tried this:
pgm = "SchTasks"
args = ['/Create /SC ONCE /TN "test" /TR "run_alert.py" /ST 07:50']
#args = ['/Create', '/SC ONCE', '/TN "test"', '/TR "run_alert.py"', '/ST
07:50']
cmd = [pgm]
cmd.extend(args)
subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
but got this error:
ERROR: Invalid argument/option - <<the above argument>>


If I use the other args list I get this error:
ERROR: Invalid argument/option - '/SC ONCE'
so apparently it liked the first argument.


Please advise.
TIA,
Tom
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121118/0ba4870e/attachment.html>

Search Discussions

  • Tim Golden at Nov 18, 2012 at 4:19 pm

    On 18/11/2012 13:48, Tom Borkin wrote:
    import subprocess
    #subprocess.call(['SchTasks /Create /SC ONCE /TN "My Tasks" /TR "C:/Program
    Files/Apache Group/Apache2/htdocs/ccc/run_alert.py" /ST 07:50'], shell=True)
    subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py"
    /ST 07:50'], shell=True)
    With either call, I get this error:
    C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py
    The system cannot find the path specified.

    Generally, with subprocess call:


    * Pass each element in the command line as a separate element in the
    list: ['SchTasks', '/Create', '/SC', 'ONCE', ... &c.]


    * Don't use shell=True unless you're running a command which is internal
    to cmd.exe (such as dir, copy, etc.). If you're running anything which
    exists as its own .exe, you shouldn't be using shell=True.


    TJG
  • Chris Rebert at Nov 18, 2012 at 4:39 pm

    On Sun, Nov 18, 2012 at 5:48 AM, Tom Borkin wrote:
    Hi,
    I have this code:

    #!\Python27\python

    import subprocess
    #subprocess.call(['SchTasks /Create /SC ONCE /TN "My Tasks" /TR "C:/Program
    Files/Apache Group/Apache2/htdocs/ccc/run_alert.py" /ST 07:50'], shell=True)
    subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py"
    /ST 07:50'], shell=True)
    With either call, I get this error:
    C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py
    The system cannot find the path specified.

    If I remove the ", shell=True" I get this:
    C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py
    C:\Program Files\Apache Group\Apache2\htdocs\ccc\cron_alert_activity.py,
    line 4, in <module>
    subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py"
    /ST 07:50'])
    File "C:\Python27\lib\subprocess.py", line 493, in call
    return Popen(*popenargs, **kwargs).wait()
    File "C:\Python27\lib\subprocess.py", line 679, in __init__ errread,
    errwrite)
    File "C:\Python27\lib\subprocess.py", line 896, in _execute_child
    startupinfo)
    WindowsError: [Error 2] The system cannot find the file specified
    The file exists in said directory. I can execute it from the cmd prompt.

    Per the docs (http://docs.python.org/2/library/subprocess.html#frequently-used-arguments
    ):
    "If passing a single string [as the `args` argument], either `shell`
    must be True (see below) or else the string must simply name the
    program to be executed **without specifying any arguments.**"
    (emphasis mine)

    So I tried this:
    pgm = "SchTasks"
    args = ['/Create /SC ONCE /TN "test" /TR "run_alert.py" /ST 07:50']
    #args = ['/Create', '/SC ONCE', '/TN "test"', '/TR "run_alert.py"', '/ST
    07:50']
    cmd = [pgm]
    cmd.extend(args)
    subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
    but got this error:
    ERROR: Invalid argument/option - <<the above argument>>

    If I use the other args list I get this error:
    ERROR: Invalid argument/option - '/SC ONCE'
    so apparently it liked the first argument.

    Please advise.

    Your tokenization of your command is incorrect. Consult the Note box
    in the docs regarding `args` tokenization, and apply it to your
    command:
    http://docs.python.org/2/library/subprocess.html#subprocess.Popen


    The-docs-are-your-friend-ly Yours,
    Chris
  • Tom Borkin at Nov 20, 2012 at 11:41 pm
    Using shlex, I now have this:
    #!\Python27\python
    import os, subprocess
    path = os.path.join("C:\\", "Program Files", "Apache Group", "Apache2",
    "htdocs", "ccc", "run_alert.py")
    #subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"',
    '/TR', path, '/ST', '23:50'])
    subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"',
    '/TR', 'run_alert.py', '/ST', '23:50'])
    Both of the above commands throw the same error:
    ERROR: The filename, directory name or volume label syntax is incorrect.


    Pls advise.
    TIA,
    Tom






    On Sun, Nov 18, 2012 at 12:39 PM, Chris Rebert wrote:

    On Sun, Nov 18, 2012 at 5:48 AM, Tom Borkin wrote:
    Hi,
    I have this code:

    #!\Python27\python

    import subprocess
    #subprocess.call(['SchTasks /Create /SC ONCE /TN "My Tasks" /TR
    "C:/Program
    Files/Apache Group/Apache2/htdocs/ccc/run_alert.py" /ST 07:50'],
    shell=True)
    subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py"
    /ST 07:50'], shell=True)
    With either call, I get this error:
    C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py
    The system cannot find the path specified.

    If I remove the ", shell=True" I get this:
    C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py
    C:\Program Files\Apache Group\Apache2\htdocs\ccc\cron_alert_activity.py,
    line 4, in <module>
    subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR
    "run_alert.py"
    /ST 07:50'])
    File "C:\Python27\lib\subprocess.py", line 493, in call
    return Popen(*popenargs, **kwargs).wait()
    File "C:\Python27\lib\subprocess.py", line 679, in __init__ errread,
    errwrite)
    File "C:\Python27\lib\subprocess.py", line 896, in _execute_child
    startupinfo)
    WindowsError: [Error 2] The system cannot find the file specified
    The file exists in said directory. I can execute it from the cmd prompt.
    Per the docs (
    http://docs.python.org/2/library/subprocess.html#frequently-used-arguments
    ):
    "If passing a single string [as the `args` argument], either `shell`
    must be True (see below) or else the string must simply name the
    program to be executed **without specifying any arguments.**"
    (emphasis mine)
    So I tried this:
    pgm = "SchTasks"
    args = ['/Create /SC ONCE /TN "test" /TR "run_alert.py" /ST 07:50']
    #args = ['/Create', '/SC ONCE', '/TN "test"', '/TR "run_alert.py"', '/ST
    07:50']
    cmd = [pgm]
    cmd.extend(args)
    subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
    but got this error:
    ERROR: Invalid argument/option - <<the above argument>>

    If I use the other args list I get this error:
    ERROR: Invalid argument/option - '/SC ONCE'
    so apparently it liked the first argument.

    Please advise.
    Your tokenization of your command is incorrect. Consult the Note box
    in the docs regarding `args` tokenization, and apply it to your
    command:
    http://docs.python.org/2/library/subprocess.html#subprocess.Popen

    The-docs-are-your-friend-ly Yours,
    Chris
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20121120/1ea7878d/attachment-0001.html>
  • Dave Angel at Nov 21, 2012 at 1:28 am
    On 11/20/2012 06:41 PM, Tom Borkin wrote:


    (Please don't top-post. Now we lose all the context)
    Using shlex, I now have this:
    #!\Python27\python
    import os, subprocess
    path = os.path.join("C:\\", "Program Files", "Apache Group", "Apache2",
    "htdocs", "ccc", "run_alert.py")
    #subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"',
    '/TR', path, '/ST', '23:50'])
    subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"',
    '/TR', 'run_alert.py', '/ST', '23:50'])
    Both of the above commands throw the same error:
    ERROR: The filename, directory name or volume label syntax is incorrect.

    I don't use Windows, but doesn't a Windows program usually have an .exe
    extension? So why would you expect it to find SchTasks ? Adding
    extensions is a shell feature, and you're not using the shell.


    Also, you should take a look at the value "path". On Linux, it shows up as:


    C:\\/Program Files/Apache Group/Apache2/htdocs/ccc/run_alert.py


    It'll be different under Windows, but probably still wrong.




    --


    DaveA
  • Prasad, Ramit at Nov 23, 2012 at 10:32 pm

    Dave Angel wrote:
    On 11/20/2012 06:41 PM, Tom Borkin wrote:

    (Please don't top-post. Now we lose all the context)
    Using shlex, I now have this:
    #!\Python27\python
    import os, subprocess
    path = os.path.join("C:\\", "Program Files", "Apache Group", "Apache2",
    "htdocs", "ccc", "run_alert.py")
    #subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"',
    '/TR', path, '/ST', '23:50'])
    subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"',
    '/TR', 'run_alert.py', '/ST', '23:50'])
    Both of the above commands throw the same error:
    ERROR: The filename, directory name or volume label syntax is incorrect.
    I don't use Windows, but doesn't a Windows program usually have an .exe
    extension? So why would you expect it to find SchTasks ? Adding
    extensions is a shell feature, and you're not using the shell.

    Also, you should take a look at the value "path". On Linux, it shows up as:

    C:\\/Program Files/Apache Group/Apache2/htdocs/ccc/run_alert.py

    It'll be different under Windows, but probably still wrong.

    Windows 7 + Python 2.6
    os.path.join("C:\\", "Program Files", "Apache Group", "Apache2",
    ... "htdocs", "ccc", "run_alert.py")
    'C:\\Program Files\\Apache Group\\Apache2\\htdocs\\ccc\\run_alert.py'




    ~Ramit




    This email is confidential and subject to important disclaimers and
    conditions including on offers for the purchase or sale of
    securities, accuracy and completeness of information, viruses,
    confidentiality, legal privilege, and legal entity disclaimers,
    available at http://www.jpmorgan.com/pages/disclosures/email.
  • Tim Golden at Nov 21, 2012 at 10:01 am

    On 20/11/2012 23:41, Tom Borkin wrote:
    Using shlex, I now have this:
    #!\Python27\python
    import os, subprocess
    path = os.path.join("C:\\", "Program Files", "Apache Group", "Apache2",
    "htdocs", "ccc", "run_alert.py")
    #subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"',
    '/TR', path, '/ST', '23:50'])
    subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"',
    '/TR', 'run_alert.py', '/ST', '23:50'])
    Both of the above commands throw the same error:
    ERROR: The filename, directory name or volume label syntax is incorrect.

    The following works for me:


    import subprocess
    path = r"C:\Program Files\Apache Group\Apache2\htdocs\ccc\run_alert.py"
    subprocess.call([
    'SchTasks', '/Create',
    '/SC', 'ONCE',
    '/TN', 'test',
    '/TR', path,
    '/ST', '23:50'
    ])


    Things to note:


    * I haven't added extra quoting to any of the items in the command list
    which is subprocess.call's first parameter. The point about using the
    list (as opposed to passing an entire string which you can also do) is
    that the subprocess module can quote things as needed. If you've already
    added quotes, you'll get double-quoting which you almost certainly don't
    want.


    * (Obviously) I've formatted the subprocess.call as I have for clarity,
    especially via email. It's just a list.


    TJG
  • Tom Borkin at Nov 21, 2012 at 9:53 pm

    On Wed, Nov 21, 2012 at 6:01 AM, Tim Golden wrote:


    subprocess.call([
    'SchTasks', '/Create',
    '/SC', 'ONCE',
    '/TN', 'test',
    '/TR', path,
    '/ST', '23:50'
    ])
    Thank you. Yes, it was the quoting of "test". Oops :-}
    Thanks again,
    Tom
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20121121/152b4fdc/attachment.html>

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedNov 18, '12 at 1:48p
activeNov 23, '12 at 10:32p
posts8
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase