简体   繁体   中英

Python execfile print none, not print value from variable

I have file 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)

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

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.

BTW, identification is important in python.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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