def saveFile(fName, data, savePolicy, magic):
    """Saves the NBT data to the disk.
    :fName: str/unicode: full file path to save data to.
    :data: object: NBT data to be saved.
    :savePolicy: int (-1, 0 or 1): special bit to handle different data types.
    :magic: int/None: 'magic' number for PE worlds."""
    if fName is None:
        return
    if os.path.exists(fName):
        r = ask("File already exists.\nClick 'OK' to choose one.")
        if r == "OK":
            folder, name = os.path.split(fName)
            suffix = os.path.splitext(name)[-1][1:]
            file_types = _("Levels and Schematics") + "\0*.dat;*.nbt"
            if "*.%s"%suffix not in file_types:
                file_types += "\0*.{0}\0*.{0}".format(suffix)
            file_types += "\0\0"
            fName = mcplatform.askSaveFile(folder, "Choose a NBT file...", name, file_types, suffix)
        else:
            return
    if fName:
        if savePolicy in (-1, 1):
            if data.get('Schematic'):
                data = data['Schematic']
            elif hasattr(data, 'name'):
                data.name = ""
        if not os.path.isdir(fName):
            if savePolicy <= 0:
                data.save(fName)
            elif savePolicy == 1:
                with littleEndianNBT():
                    # Here we have a PE data file. Just strip out the 
                    toSave = data.save(compressed=False)
                    toSave = struct.Struct('<i').pack(magic) + struct.Struct('<i').pack(len(toSave)) + toSave
                    with open(fName, 'wb') as f:
                        f.write(toSave)
        else:
            alert("The selected object is not a file.\nCan't save it.")
Saves the NBT data to the disk.
    :fName: str/unicode: full file path to save data to.
    :data: object: NBT data to be saved.
    :savePolicy: int (-1, 0 or 1): special bit to handle different data types.
    :magic: int/None: 'magic' number for PE worlds.
editortools/nbtexplorer.py