#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    How to Edit a vmx file offline using python


    Hi,

    My file sample.vmx contains data

    pciBridge7.virtualDev = "pcieRootPort"
    pciBridge7.functions = "8"
    vmci0.present = "TRUE"
    hpet0.present = "TRUE"
    nvram = "testvmdk.nvram"
    virtualHW.productCompatibility = "hosted"
    powerType.powerOff = "soft"
    powerType.powerOn = "hard"


    I need to update those values and the updated file should contains the data

    pciBridge7.virtualDev = "pcieRootPort"
    pciBridge7.functions = "8"
    vmci0.present = "TRUE"
    hpet0.present = "TRUE"
    nvram = "testing.nvram"
    virtualHW.productCompatibility = "hosted"
    powerType.poweroff='hard"
    powerType.poweron="sof't"


    Can anyone please help me in solving this problem

    Thanks,
    Sasikiran

    And then the
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    This will give you a new file with the updated values, but it's simple enough to change it to overwrite the old file.

    Code:
    new_lines = []
    with open('vmx.vmx') as f:
        for line in f.readlines():
            parts = line.split("=")
            key = parts[0].strip()
            if key == 'powerType.powerOff':
                value = '"hard"'
            elif key == 'powerType.powerOn':
                value = '"soft"'
            else:
                value = parts[1].strip()
            new_lines.append("%s = %s" % (key, value))
    
    with open('vmx.vmx2', 'wb') as f2:
        for line in new_lines:
            f2.write("%s\n" % line)

IMN logo majestic logo threadwatch logo seochat tools logo