Python execfile print none, not print value from variable

43 Views Asked by At

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

1

There are 1 best solutions below

2
B.Gu On

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.