-2

I have created a python script that works really well. It opens a .txt file and picks a word a random before using that for a quiz. If I want to share this code do I need to share the .txt file also and does the receiver have to change the path to the .txt file in the code to the location of the txt file on their computer?

Other than putting the words as a list directly in the code is there a way of referencing the .txt file to the code in such a way that it will work on any computer?

Gtbiyb
  • 3
  • 1
  • Make it user configurable via CLi argument -> argparse – Marcin Orlowski Aug 08 '21 at 07:17
  • 1
    Yes, you also need to share this .txt-file. If you put it in the same directory you can open it using _relative paths_: `./yourtextfile.txt` – Ocaso Protal Aug 08 '21 at 07:17
  • if the txt file has a relative path then you will be able to access it on any computer given that it is where it should be relative to the accessing script – Matiiss Aug 08 '21 at 07:19
  • 1
    @OcasoProtal not exactly, a relative path is relative *to the working directory*, this assumes that the script is run with the source code file in the working directory, but that isn't always the case – juanpa.arrivillaga Aug 08 '21 at 07:43
  • @juanpa.arrivillaga You are correct, but for a beginner that is mostly the same directory, otherwise use https://stackoverflow.com/questions/5137497/find-current-directory-and-files-directory – Ocaso Protal Aug 08 '21 at 07:46
  • Thank you all for taking time to respond – Gtbiyb Aug 10 '21 at 03:07

1 Answers1

0

You can change the hardcoded path and replacing it using a variable that will reference the file path to your text file and ask for the input when the scripts, in various ways,

  1. use input() to ask for user input
  2. read as an CLI argument that user triggers the script
  3. maintain a user configuration file and have the script read it
  4. ...

whatever ways you can think of that is efficient and suitable for your use case

kennysliding
  • 2,783
  • 1
  • 10
  • 31