-2

this is my code, i'm trying to convert it to .exe, it works but does not open and read the text files, i did it on MacOs and works well, but does not work in windows

the problem is in this part

def make_house():
    try:
        server_Adress = entry_server.get()
        pythonpPort = int(entry_port.get())
        playerName = entry_player.get()

        mc = Minecraft.create(server_Adress, pythonpPort, playerName)

        xp,yp,zp=mc.player.getPos()
        print("yes")
        # file_name = os.getcwd() + '/house.txt'
        print("yes")
        my_file = open("/Users/me/Desktop/Python/python-projects/house.txt","r")
        print("yes")
        print(my_file)
        for line in my_file:
            nums = line.strip().split(" ")
            x = int(nums[0])  + xp
            y = int(nums[1])  + yp
            z = int(nums[2])  + zp
            b = int(nums[3]) 
            mc.setBlock(x, y, z, b)  
        print("done")
    except:
        showerror(
            "error", "please check your server address , port and player name")

the command i used:

pyinstaller filename.py --add-data 'pyramid.txt:.' --add-data 'house.txt:.' --windowed --onefile -i logo.ico

fatemeh
  • 1
  • 1

1 Answers1

0

In your pyinstaller command you indicate that you want to compile the house.txt file with your app, but then in the code you give it an absolute path to a subdirectory on your user Desktop.

If you want to use the file compiled with the executable you need to create a relative path to the runtime directory created by the executable when executed using the __file__ attribute.

For example:

def make_house():
    try:
        server_Adress = entry_server.get()
        pythonpPort = int(entry_port.get())
        playerName = entry_player.get()
        mc = Minecraft.create(server_Adress, pythonpPort, playerName)
        xp,yp,zp=mc.player.getPos()
        print("yes")
        parent = os.path.dirname(__file__)  # parent is the runtime dir
        file_name = os.path.join(parent, 'house.txt') # this is the file you compiled with the app
        print("yes")
        # my_file = open(file_name)
        print("yes")
        # print(my_file)
        with open(file_name) as my_file:  # you should get into practice of opening files like this
            for line in my_file:
                nums = line.strip().split(" ")
                x = int(nums[0])  + xp
                y = int(nums[1])  + yp
                z = int(nums[2])  + zp
                b = int(nums[3]) 
                mc.setBlock(x, y, z, b)  
            print("done")
    except:
        showerror(
            "error", "please check your server address , port and player name")
Alexander
  • 16,091
  • 5
  • 13
  • 29