(I’m not an advanced coder, so thank you in advance for your patience.)
I uploaded a list of words as a .txt as an asset. I would like to open this file in the Server, use it to create a list, and then send a couple of the items in this list to the Form1 (Client).
I used a snippet of code from the Documentation to create this:
path = “_/theme/file.txt” —> this is what I get when I click “copy asset path”
with open(path, ‘r’) as f:
contents = f.read()
list = []
for i in contents:
list.append(i)
When running the code I get a `FileNotFoundError: [Errno 2] No such file or directory: ’ message, so I’m obviously doing something wrong. Any hints to get me into the right path (pun intended)?
Yeah, I read that, unfortunately, this doesn’t tell me anything (i.e.: I can’t see how it applies), but maybe that is because I’m not very experienced.
Thanks anyway
That post gives you the exact code you need to use. Look at the two lines of code given. The first line sets up a URL to the file in the assets. The second line reads the contents of that file as bytes. Basically what you’re trying to do. What you’re missing is that you cannot use a relative URL to the file, you must fully qualify it using get_app_origin.
But yes, if you’re intending to use the .txt file on the server, as Phil notes the data files service is a more straightforward way to go about it these days, and has the advantage that you don’t incur the overhead of sending the file to the client.
P.Colbert’s suggestion, from what I can see, requires a paid plan, which I don’t (yet?) have.
Reading a bit more, I can now understand the example and I am indeed able to get the data in ‘bytes’ format. Thanks.
But, now I’m struggling to understand how can I translate the bytes into something useful, e.g., a list, or a string, or an iterable object. Any suggestion? (Again, thank you for your patience with these level 1 questions)
That’s a standard Python question that I always have to look up since I do it so rarely. I’m pretty sure it involves calling decode on the bytes, something like get_bytes().decode('utf-8') to get a string out of it.