如何正确传递要使用 python -c 执行的文字字符串 python 代码
我需要一些帮助来弄清楚如何从 python -c 执行此 python 代码 我在格式化 python 时遇到问题,以便它将在 cmd 中为另一个应用程序执行 我知道可能还有其他方法可以完成我正在做的事情,但我受到使用 cmd python -c 最终执行的限制,所以请记住这一点。
所以我有一些 python 代码,
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))
但我试图使用以下方法来执行它, python -c "print 'hotdogs'"
所以这就是我所拥有的,但没有工作
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))"
Windows CMD
python -c "+ cmdline +'"'
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.
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))"
Windows CMD
python -c "+ cmdline +'"'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你真的需要做一个单行黑客(这是完全不建议或宽恕)那么你可以这样做
但是你真正应该做的是使它成为一个位于你的Windows路径内的脚本,然后你可以做
这似乎是好多了,对吧?
If you really really need to do a one line hack (this is totally not suggested or condoned) then you can do this
But what you should really do is make this a script that is inside your windows path, then you can do
Which seems a lot nicer right?
使用 python -c 的 Powershell 指南
"
中 - 以便您计算其中的 PowerShell 特殊字符(对于换行符和制表符),'
作为代码中的引号部分`n
作为行分隔符`t
作为 python 缩进所必需的制表符示例:minify json oneliner
foo.json
作为输入并将其缩小版本打印到控制台,Powershell guidelines for using python -c
"
- so that you evaluate PowerShell special characters within (for newlines and tabs)'
for quotes within the code section`n
as line separators`t
as tab character essential for python indentationExample: minify json oneliner
foo.json
as an input and prints it minified version to the console对于 CMD,请将整个代码部分括在双引号“内。否则它将无法工作(无输出)。
For CMD, enclose the whole code section within double quotes ". Else it will simply not work (no output).