Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
file storage and iCloud sync question
-
I’ve created a script in “Script Library/iCloud” using pythonista. The script creates a file and saves some cached geolocation data using the below code.
my_people = {} my_people_file = 'contacts-data.json' # populate my_people dictionary with data def save_cache(): json_text = json.dumps(my_people) with open(my_people_file, 'w') as f: f.write(json_text)
This seemed to work fine initially. I only run the script from a single device. I run this script which builds the dictionary with geolocation data and saves it to the file. Once the cache file is built, I have another script which loads the cache file and uses it for other purposes. Once the cache file is created it is not written again, it’s only read from the other script. In other words, I'm not repeatedly writing to this file or doing so from multiple devices.
I noticed, sometime later, that the cache file created in “Script Library/iCloud” had been renamed from ‘contacts-data.json' to ‘contacts-data 2.json'.
My thought was some sort of cloud sync issue but as I said I only am using a single device and only writing it once. I store the scripts in the iCloud area so they’re backed up. Since the script is there, saving the file with the above also saves it there in the same area.
Trying to manually rename it and remove the ‘ 2’ doesn’t seem to work. It won’t let me change the name. When this happens I have to manually delete the cache file and run the script to rebuild it.
Anyone else run into this? I wonder if I need to direct the saving of this cache file to the device local area despite the script running from the iCloud area. Is that possible? Is there a special path prefix I can use when saving the file from python which will direct it to local storage instead of iCloud? Is there a known issue saving a file from a script using the code I provided when that script itself is saved in the “Script Library/iCloud” area?
-
def save_cache(): with open(my_people_file, 'w') as out_file: json.dump(my_people, out_file)
-
@nerdtronn we now that iCloud sync gives a lot of problems.
When you rerun your script, if the target file is still busy to upload to iCloud, iOS creates a -2 etc...
I have even 30 versions of some files. If you want to rename it, it is not possible because iOS manages it-self these names. Don't ask me why. What I do is to always create a new file with its name containing the date/time of creation, so you are sure that the file name does not exist already.
You store this name somewhere and you delete the previous file. -
@ccc Can you elaborate on that? That looks similar to what I have, though, a little more compact. Are you saying that addresses the cloud sync issue here?
@cvp Thanks! That sounds like an interesting approach. I could append something unique each time and then when loading load the one with the largest unique value. I'll give that a try.