简体   繁体   English

Python execfile不打印任何内容,不打印变量的值

[英]Python execfile print none, not print value from variable

I have file mikrotik.py 我有文件mikrotik.py

'ip route add dst-address={}/{} gateway={}'.format(destination,prefix,gateway)

and this some code 还有一些代码

import os
    localfilepath = os.getcwd()
    staticDir = localfilepath+"/website/plugin/config/routing/static/"
    vendors = staticDir+"MIKROTIK.py"
    destination = 192.168.2.0
    prefix = 24
    gateway = 192.168.1.1
    x = execfile(vendors)
    print x

the result is 结果是

None

I want the result is 我想要结果是

ip route add dst-address=192.168.2.0/24 gateway=192.168.1.1

When I use 当我使用

x = open(vendors)
print x

the result is 结果是

'ip route add dst-address={}/{} gateway={}'.format(destination,prefix,gateway)

Thanks in advance 提前致谢

so finally i use eval(x) 所以最后我用eval(x)

and the result is ip route add dst-address=192.168.2.0/24 gateway=192.168.1.1, i dont know its the best way or no 结果是IP路由添加dst-address = 192.168.2.0 / 24网关= 192.168.1.1,我不知道它的最佳方法

If I understand correctly, you only need simple string manipulation: 如果我理解正确,则只需要简单的字符串操作即可:

import os

destination = 192.168.2.0
prefix = 24
gateway = 192.168.1.1
vendors = 'ip route add dst-address={}/{} gateway={}'.format(destination,prefix,gateway)
print x

If you really want to put the string/format in a separate file, use text file, json file etc, but no .py file. 如果您确实要将字符串/格式放在单独的文件中,请使用文本文件,json文件等,但不要使用.py文件。

BTW, identification is important in python. 顺便说一句,识别在python中很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM