1

I have created an executable file of a python script that uses a text file a.txt. I have created a one file executable using the script file bot.py and the text file a.txt together. However, the executable only works when it is in the same folder as bot.py and a.txt files. If it is in a folder without these two files it doesn't work and throws an error.

My code:

subs = []

with open("a.txt") as file:

    for line in file:

        if line != "\n":
            x = line.strip()

            subs.append(x)

What can I do to make sure that it works even when the original 2 files are not present?

This is my first post here, so sorry if its not very concise. Thanks in advance

  • Please check this and edit your question, or it will be downvoted to oblivion :) https://stackoverflow.com/help/how-to-ask – Rafael Jun 18 '22 at 12:51
  • Also, if you are asking a code related question, show the code, that is related to the question subject. You don't need to publish all code listing, only the part, that is involved in the question subject. And again, check this before asking questions: https://stackoverflow.com/help/how-to-ask – Rafael Jun 18 '22 at 12:52
  • The answer is actually in the question: "However, the executable only works when it is in the same folder as the script and the text files" This is known as a "path" issue. The computer looks for the 3 files in the same directory by default. To get different results you must manage where the exe looks for files and where the script looks for the text file. – Carl_M Jun 18 '22 at 12:54
  • @Carl_M Could you possible help me with that? Or suggest any resources that I can use to learn how to do this? I've just started learning python so I haven't yet learnt how to do that. – BeginnerCode776 Jun 18 '22 at 13:46
  • Welcome to python and StackOverflow. I have posted an answer below which uses python's [input function](https://docs.python.org/3/library/functions.html#input). – Carl_M Jun 18 '22 at 15:25
  • Try converting it to a single executable file. – Eshaan Gupta Jun 18 '22 at 18:10
  • @Carl_M I'm trying to make a single executable file so that the user doesn't need to have the text file on their laptop. I thought if you used auto py to exe, you can create one executable file, that INCLUDES the text file inside it? Basically I dont want people to have access to the text file or the program, but just be able to execute the script. Is that even possible? – BeginnerCode776 Jun 19 '22 at 03:39
  • "However, the executable only works when it is in the same folder as bot.py and a.txt files. If it is in a folder without these two files it doesn't work and throws an error." According to the [auto-py-to-exe page](https://github.com/brentvollebregt/auto-py-to-exe) you just add files to be included in the exe. Note auto-py-to-exe is a Graphical User Interface (GUI) to pyinstaller. You might want to read the [pyinstaller documentation](https://pyinstaller.org/en/v5.0.1/operating-mode.html). Maybe even try pyinstaller directly. That all said you are definitely attempting something challenging. – Carl_M Jun 19 '22 at 13:20

1 Answers1

1
"""
Assume the exe and bot.py are in the same folder.

Then we need to handle the path to the text file.

Use an input statement to get the path.
"""

subs = []
# Respond to the input prompt with a complete path.
# That file will then be processed.
file_to_process = input("Enter the full path of the file to process. ")
with open(file_to_process) as file:

    for line in file:

        if line != "\n":
            x = line.strip()

            subs.append(x)

Example run filled the subs list.

Enter the full path of the file to process. C:\Users\USERNAME\Desktop\words.txt
Carl_M
  • 861
  • 1
  • 7
  • 14