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

Jean-Michel Pichavant jeanmichel at sequans.com
Tue Mar 1 05:36:29 EST 2011


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



More information about the Python-list mailing list