c传字符串给python_如何正确传递文字字符串python代码以使用python -c执行

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 +'"'

解决方案

If you really really need to do a one line hack (this is totally not suggested or condoned) then you can do this

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))"

But what you should really do is make this a script that is inside your windows path, then you can do

python myfancyscript.py

Which seems a lot nicer right?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值