how to properly pass literal strings python code to be executed usingpython -c

Discussion in 'Python' started by jmoons, Feb 28, 2011.

  1. jmoons

    jmoons Guest

    I need some help figuring out how to execute this python code from
    python -c
    I am have trouble formatting python so that it will execute for
    another app in cmd I understand there maybe other ways to do what I am
    doing but I am limited by the final execution using cmd python -c so
    please keep this in mind.
    I'm limited by the final delivery of code. The python is being called
    by a server that does not have access to any python script file

    So I have some python code ie,

    import os
    import shutil

    myPath =r"C:\dingdongz"
    for root, dirs, files in os.walk(myPath):
    for file in files:
    os.remove(os.path(root, file))
    for dir in dirs:
    shutil.rmtree(os.path.join(root,dir))

    But I am trying to excute it using the following method, python -c
    "print 'hotdogs'"

    So this what i have but no worky

    cmdline = "\" import os, shutil \n for root, dirs, files in
    os.walk("+myPath+"):\n \t for file in files: \n \t \t
    os.remove(os.path.join(root, file)) \n \t for dir in dirs: \n \t\t
    shutil.rmtree(os.path.join(root, dir))"


    I have also tried the following
    python -c "import os; import shutil; for root, dirs, files in
    os.walk('+myPath+'): for file in files: os.remove(os.path.join(root,
    file)); for dir in dirs: shutil.rmtree(os.path.join(root, dir))"

    I am still getting error tree(os.path.join(root, dir)) ^ SyntaxError:
    invalid syntax
    jmoons, Feb 28, 2011
    #1
    1. Advertising

  2. Re: how to properly pass literal strings python code to be executedusing python -c

    On Mon, 28 Feb 2011 10:59:01 -0800, jmoons wrote:

    > I need some help figuring out how to execute this python code from
    > python -c
    > I am have trouble formatting python so that it will execute for another
    > app in cmd I understand there maybe other ways to do what I am doing but
    > I am limited by the final execution using cmd python -c so please keep
    > this in mind.
    > I'm limited by the final delivery of code. The python is being called by
    > a server that does not have access to any python script file


    Let me translate that...

    "I'm having trouble hammering this nail with a screwdriver. Keep in mind
    that I am limited by the requirement that I use a screwdriver, not a
    hammer, to hammer the nail. The nail is being hammered by somebody who
    doesn't have a hammer."

    So give them a hammer. Put the code in a text file, call it "main.py" or
    something, and execute "python -m main", or "python -c 'import main'" if
    you prefer.

    I don't understand the requirement to avoid storing your code in a file
    -- surely you won't be typing the script into cmd every single time you
    want to run it, so surely it will be stored in a batch file or similar?
    As far as I can tell, the *only* legitimate reason for the requirement is
    to win a bet :) Otherwise, you're just making your life much much harder
    than it needs to be.


    [...]
    > So this what i have but no worky
    >
    > cmdline = "\" import os, shutil \n for root, dirs, files in
    > os.walk("+myPath+"):\n \t for file in files: \n \t \t
    > os.remove(os.path.join(root, file)) \n \t for dir in dirs: \n \t\t
    > shutil.rmtree(os.path.join(root, dir))"



    I have no idea what the string handling rules for cmd are, and I'm not
    going to try to guess. This doesn't appear to be a Python problem, it's a
    cmd problem. You need to work out how to correctly quote your string.
    Perhaps try on some Windows forums.


    > I have also tried the following
    > python -c "import os; import shutil; for root, dirs, files in
    > os.walk('+myPath+'): for file in files: os.remove(os.path.join(root,
    > file)); for dir in dirs: shutil.rmtree(os.path.join(root, dir))"
    >
    > I am still getting error tree(os.path.join(root, dir)) ^ SyntaxError:
    > invalid syntax


    No you don't. You don't call a function "tree", so you can't be getting
    that error. The actual function you call is shutil.rmtree. Please don't
    retype, summarize, simplify or paraphrase error messages. Copy and paste
    them *exactly* as they are shown, complete with any traceback which is
    printed.


    --
    Steven
    Steven D'Aprano, Feb 28, 2011
    #2
    1. Advertising

  3. Re: how to properly pass literal strings python code to be executedusing python -c

    jmoons wrote:
    > I need some help figuring out how to execute this python code from
    > python -c
    > I am have trouble formatting python so that it will execute for
    > another app in cmd I understand there maybe other ways to do what I am
    > doing but I am limited by the final execution using cmd python -c so
    > please keep this in mind.
    > I'm limited by the final delivery of code. The python is being called
    > by a server that does not have access to any python script file
    >

    Why ? You are about to delete a whole directory tree on that ser ver and
    cannot write a simple python file ?
    > So I have some python code ie,
    >
    > import os
    > import shutil
    >
    > myPath =r"C:\dingdongz"
    > for root, dirs, files in os.walk(myPath):
    > for file in files:
    > os.remove(os.path(root, file))
    > for dir in dirs:
    > shutil.rmtree(os.path.join(root,dir))
    >
    > But I am trying to excute it using the following method, python -c
    > "print 'hotdogs'"
    >
    > So this what i have but no worky
    >
    > cmdline = "\" import os, shutil \n for root, dirs, files in
    > os.walk("+myPath+"):\n \t for file in files: \n \t \t
    > os.remove(os.path.join(root, file)) \n \t for dir in dirs: \n \t\t
    > shutil.rmtree(os.path.join(root, dir))"
    >
    >
    > I have also tried the following
    > python -c "import os; import shutil; for root, dirs, files in
    > os.walk('+myPath+'): for file in files: os.remove(os.path.join(root,
    > file)); for dir in dirs: shutil.rmtree(os.path.join(root, dir))"
    >
    > I am still getting error tree(os.path.join(root, dir)) ^ SyntaxError:
    > invalid syntax
    >

    I don't understand exactly what you want to do... Anyway ...

    1/ Your 'application' is running on the server, it has then access to
    the file system and can write python file => problem solved.
    2/ Your 'application' is local and sends python code to the server =>
    write a local python file, that uses the module 'execnet' to execute
    remote python code on the server. See http://codespeak.net/execnet/


    JM
    Jean-Michel Pichavant, Mar 1, 2011
    #3
  4. jmoons

    jmoons Guest

    Re: how to properly pass literal strings python code to be executedusing python -c

    On Feb 28, 3:14 pm, Steven D'Aprano <steve
    > wrote:
    > On Mon, 28 Feb 2011 10:59:01 -0800, jmoons wrote:
    > > I need some help figuring out how to execute this python code from
    > > python -c
    > > I am have trouble formatting python so that it will execute for another
    > > app in cmd I understand there maybe other ways to do what I am doing but
    > > I am limited by the final execution using cmd python -c so please keep
    > > this in mind.
    > > I'm limited by the final delivery of code. The python is being called by
    > > a server that does not have access to any python script file

    >
    > Let me translate that...
    >
    > "I'm having trouble hammering this nail with a screwdriver. Keep in mind
    > that I am limited by the requirement that I use a screwdriver, not a
    > hammer, to hammer the nail. The nail is being hammered by somebody who
    > doesn't have a hammer."
    >
    > So give them a hammer. Put the code in a text file, call it "main.py" or
    > something, and execute "python -m main", or "python -c 'import main'" if
    > you prefer.
    >
    > I don't understand the requirement to avoid storing your code in a file
    > -- surely you won't be typing the script into cmd every single time you
    > want to run it, so surely it will be stored in a batch file or similar?
    > As far as I can tell, the *only* legitimate reason for the requirement is
    > to win a bet :) Otherwise, you're just making your life much much harder
    > than it needs to be.
    >
    > [...]
    >
    > > So this what i have but no worky

    >
    > > cmdline = "\" import os, shutil \n for root, dirs, files in
    > > os.walk("+myPath+"):\n \t for file in files: \n \t \t
    > > os.remove(os.path.join(root, file)) \n \t for dir in dirs: \n \t\t
    > > shutil.rmtree(os.path.join(root, dir))"

    >
    > I have no idea what the string handling rules for cmd are, and I'm not
    > going to try to guess. This doesn't appear to be a Python problem, it's a
    > cmd problem. You need to work out how to correctly quote your string.
    > Perhaps try on some Windows forums.
    >
    > > I have also tried the following
    > > python -c "import os; import shutil; for root, dirs, files in
    > > os.walk('+myPath+'): for file in files: os.remove(os.path.join(root,
    > > file)); for dir in dirs: shutil.rmtree(os.path.join(root, dir))"

    >
    > > I am still getting error tree(os.path.join(root, dir)) ^ SyntaxError:
    > > invalid syntax

    >
    > No you don't. You don't call a function "tree", so you can't be getting
    > that error. The actual function you call is shutil.rmtree. Please don't
    > retype, summarize, simplify or paraphrase error messages. Copy and paste
    > them *exactly* as they are shown, complete with any traceback which is
    > printed.
    >
    > --
    > Steven


    Thank you steven for trying to help, I appreciate trying to understand
    my questions it was hard to articulate through writing I can see know
    with out writing a page regarding the envorment in which I need to
    complete my task, there is no way for you to help.
    I will hit the windows forums on the cmd formatting.
    Thank you
    -Jentzen
    jmoons, Mar 1, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    739
    Malcolm
    Jun 24, 2006
  2. Norman Khine
    Replies:
    0
    Views:
    219
    Norman Khine
    Jun 30, 2007
  3. Wolfgang Meiners
    Replies:
    6
    Views:
    328
    Wolfgang Meiners
    May 3, 2010
  4. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    364
    Anonieko Ramos
    Sep 27, 2004
  5. Íéêüëáïò Êïýñáò
    Replies:
    1
    Views:
    110
    Íéêüëáïò Êïýñáò
    Jun 4, 2013
Loading...

Share This Page