1

I am writing a Python program that reads a file and based on that information and inputs provided by the user perform a task.The file is always the same and it is not modified by the user. How can I import/include the file in my program such that the users won't have to copy the file in their directory for the program to work? This is what mainly I am trying to avoid.

Tesla001
  • 493
  • 1
  • 5
  • 7
  • Is your program going to run from the user's directory, or be imported? – Reblochon Masque Aug 03 '18 at 04:19
  • Your Python application *will* have to know where the file is. You have a two options: move the data in the file into the python application, or install the file into a known location on the system. – ktb Aug 03 '18 at 04:26
  • Is the file a plain text file? –  Aug 03 '18 at 04:32
  • The ideal solution is to design you program to be installed—whether via `pip`, or something like a WIndows installer/Mac DMG/etc. as built by something like `PyInstaller`. Then, you use the `setuptools` [Resource Manager API](https://setuptools.readthedocs.io/en/latest/pkg_resources.html#resourcemanager-api). `setuptools` makes sure that, wherever the file gets installed (even if it's buried inside a `.exe` file), you can just call `resource_stream` to open it (or just call `resource_string` to get its contents). – abarnert Aug 03 '18 at 04:39
  • [This question](https://stackoverflow.com/questions/1395593/managing-resources-in-a-python-project) shows how to do it with `setuptools` pretty nicely. – abarnert Aug 03 '18 at 04:41
  • @ReblochonMasque The program is going to run from the user's directory. – Tesla001 Aug 03 '18 at 04:50
  • @ktb can you please explain more what do you mean about moving the data into the python application ? – Tesla001 Aug 03 '18 at 04:54
  • See @Zizouz's answer – ktb Aug 03 '18 at 11:32

2 Answers2

0

Assuming that your program is a single script, and the file is constant you can even include it in your script (note the triple quotes for multi-line data):

information = '''
ALL 
THE INFORMATION GOES HERE!
'''

If the information you need to store contains any binary data, or characters such as a backslash (\), you can store the data instead in base 64, and decode the data on import:

import base64

information = 'CkFMTApUSEUgSU5GT1JNQVRJT04gR09FUyBIRVJFIQo='
information = base64.b64decode(information)
Zizouz212
  • 4,908
  • 5
  • 42
  • 66
0

Is it fine if the program downloads that information file from the internet? If so, you can use the information provided here: https://developers.google.com/drive/api/v3/quickstart/python

Then, you could just upload the information to Drive and have the user download as needed.

Elsewise, follow the advice from the answer before me.

EntangledLabs
  • 115
  • 1
  • 9