T
T
therealxado2022-01-28 20:08:31
Python
therealxado, 2022-01-28 20:08:31

How to specify file paths in Kivy?

Good hour! Please tell me if in native python on a PC I can access files like this: file = open('File.txt', 'r', encoding = 'utf-8'), that is, I work with a file that is in a folder with main.py, then how to specify it for a ready-made android application?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vindicar, 2022-01-28
@therealxado

file = open('File.txt', 'r', encoding = 'utf-8'), that is, I work with a file that is in the folder with main.py,

Not right. You are working with a file that is in the current working directory because you specified a relative path (i.e. not from the root of the drive).
The current working directory may or may not be the same as the directory where the script resides. It depends on the behavior of the program that the script launches, whether it's Windows Explorer, the command line, or something else. In addition, the program can change its current working directory if desired.
In other words, you have no control over what the working directory will be at the time the program starts - unless you change it yourself.
So it's better to specify an absolute path. The easiest way is to get the path to the script from sys.argv[0] and go up a level. That's when you get the path to the folder where your script is. And move away from this path.
Path operations are most conveniently done with pathlib , or the old fashioned way with os.path .

S
Sergey Karbivnichy, 2022-01-28
@hottabxp

What does Python have to do with it? To do something more serious than hello world on Kivy, python is not enough. You need to know the Android architecture very well. And I will tell you a secret that there is a big difference between desktop OS and mobile OS. If you don't know the Android architecture, you can forget about Kivy!
If you have not changed your mind, I recommend starting here - developer.android.com
Then this:
File
FileSystem
Data and file storage overview
When you read this, and if your interest does not fade, then before reading the file, try to request the necessary permissions to access fs - Request app permissions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question