Kivy is a python framework you can use to develop apps for Linux, Windows, OS X, Android and iOS. It’s very common to create your folder to store some information which you can access across sessions. But things always get interesting in a across platforms framework since different behaviours for each platforms. Let’s discuss them all.
Don’t combine the path and file name with a hard-code value. Always use the python built-in method
path.join() to handle this. The reason is simple, different system has different representation for folder,
And a better approach is store the path and file_name in a separate module then import it when you need it. So when you need to change it, you don’t need to change everywhere.
Then you can create folders or files to store your information.
You may think that it should create a folder within your app’s current folder. Yes, it is. But iOS doesn’t like it. It will complain you don’t have sufficient permission.
I used the current approach to get rid of the permission warning. The idea is simple, although you are using the absolute folder, you are actually in your own app’s folder, so you should have the permission.
# It will create a 'cache' folder inside your app's folder
I still get the permission problem at some stage. Then I realize there is a
user_data_dir API in Kivy which will give you the current app folder depending on different platforms. I read the source code, it’s a property. So you could access it via
But you’ll have a problem. Which is when you have some initializing logic which you happen to separate in another module. It may not work, since when you load that module, the main app won’t even there. It will give you an error like:
AttributeError: 'NoneType' object has no attribute 'user_data_dir'
You can import module in your code block rather than at the head of you
.py file, but it may break your logic.
It turns out that the only way to access it is in the
Or you can use
getattr(self, 'user_data_dir') to get the value as well. But the above way is more easy.
Thanks for reading!
Follow me (albertgao) on twitter, if you want to hear more about my interesting ideas.