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

jmoons jentzen.mooney at gmail.com
Tue Mar 1 09:45:52 EST 2011


On Feb 28, 3:14 pm, Steven D'Aprano <steve
+comp.lang.pyt... at pearwood.info> 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



More information about the Python-list mailing list