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

J

jmoons

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
 
S

Steven D'Aprano

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.
 
J

Jean-Michel Pichavant

jmoons said:
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
 
J

jmoons

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.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top