Home > Mobile >  Edit Minecraft .dat File in Python
Edit Minecraft .dat File in Python

Time:10-01

I'm looking to edit a Minecraft Windows 10 level.dat file in python. I've tried using the package nbt and pyanvil but get the error OSError: Not a gzipped file. If I print open("level.dat", "rb").read() I get a lot of nonsensical data. It seems like it needs to be decoded somehow, but I don't know what decoding it needs. How can I open (and ideally edit) one of these files?

Thank you all so much in advance.

CodePudding user response:

You'll have to give the path either relative to the current working directory

path/to/file.dat

Or you can use the absolute path to the file

C:user/dir/path/to/file.dat

Read the data,replace the values and then write it

# Read in the file
with open('file.dat', 'r') as file :
  filedata = file.read()

# Replace the target string
filedata = filedata.replace('yuor replacement or edit')

# Write the file out again
with open('file.dat', 'w') as file:
  file.write(filedata)

Hope this helps

CodePudding user response:

It seems that you are reading data in binary format using "rb". You just want to read the contents of the file using "r" like this :

with open('level.dat', 'r') as f :
  leveldata = f.read()

Editing will depend on what you want to do... Maybe you want to replace all the content inside that file, or maybe a specific line or a specific part of each line. Pehaps you just need to append more content.

  • Related