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.
Can’t write file to iPad
-
Hi
I’m using OpenAI to generate code.
I have tried numerous ways to have a python program save a .txt file to the iPad but with no success.
Is it actually possible to use a Pythonista3 program to save a file to the IPad?Thanks
James -
@SpideryArticle what have you tried? And is your script in the main documents folder. Or under an on iCloud folder?
You can write a file,but there are limits on which paths are writable. Generally
with open('test.txt','wt') as f: f.write('it works')
Should work. If you want to write to a specific location (not the script path) you need to specify a writable absolute path. You cannot write to the root folder for example.
-
from pathlib import Path Path('SpideryArticle.txt').write_text('SpideryArticle was here!')
-
@ccc Thank you so much for your help. I ran your code and it appears to run properly, but I cannot find the file Spideryarticle.txt anywhere on the iPad.
When I select the Files folder on the iPad and then do a search. I cannot find the file that was created. -
@SpideryArticle The created file is a local Pythonista file and these files are not visible in the Files app.
-
@cvp Super thanks for that.
So is there a way that I can access the files so that I can open them and read them? -
@SpideryArticle Instead of running your script in Pythonista Documents, copy it and run it in Pythonista iCloud, then you can see it in Files/iCloud Drive/Pythonista 3
-
@cvp IIRC, it needs to be in a folder inside iCloud, rather than in the "root" iCloud, otherwise you won't have write access?
Also, text files can be viewed from within pythonista. Just swipe from left to see folders, and choose your file.
-
@cvp I ran this program, which is designed to create a text file and save it in iCloud Drive, Pythonista3 folder, but I got an error, so I’m still doing something wrong:
import os # Define the file path file_name = "hello_1.txt" folder_name = "Pythonista3" file_path = os.path.expanduser(f"~/Documents/iCloud Drive/{folder_name}/{file_name}") # Create the file and write to it with open(file_path, "w") as f: f.write("Hello Jim") print("File created successfully!")
THE ERROR I GOT:
Traceback (most recent call last): File "/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/Untitled_2.py", line 9, in <module> with open(file_path, "w") as f: FileNotFoundError: [Errno 2] No such file or directory: '/private/var/mobile/Containers/Shared/AppGroup/B42BE5A2-493D-4659-B268-0AB7FAA58A75/Pythonista3/Documents/iCloud Drive/Pythonista3/hello_1.txt'
-
@JonB thanks for your assistance. Still not there yet as I have not yet been successful in getting a program to save a text file to a folder within the iCloud Drive.
-
@JonB said
it needs to be in a folder inside iCloud, rather than in the "root" iCloud, otherwise you won't have write access?
I have a lot of files in Pythonista iCloud root, seen in Files iCloud Drive/Pythonista 3 without passing by a folder
Also, text files can be viewed from within pythonista. Just swipe from left to see folders, and choose your file.
Agree but he wants to see his text files in Files App, not in Pythonista
-
@SpideryArticle No, I mean that you have to create your script in Pythonista iCloud. Try with the script of @JonB , run it and you would see your .txt file in Files app iCloud Drive/Pythonista 3
And if you want to create an iCloud file from a local Pythonista script, do like
file_name = "hello_1.txt" file_path = f"/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/{file_name}" # Create the file and write to it with open(file_path, "w") as f: f.write("Hello Jim") print("File created successfully!")
-
# Assumes that this file is in the Pythonista iCloud Documents folder. from pathlib import Path here = Path(__file__).parent # print(here.resolve()) (here / "hello_icloud.txt").write_text("On iCloud!")
It would be a nice feature if @omz could build us an symlink between
Path(".") / "iCloud"
and that cryptic path so scripts in the usual directory could read from and write to iCloud. -
@ccc we can always use editor.get_path() for a script existing in Pythonista iCloud.
-
-
@SpideryArticle 👍and welcome here.
-
we can always use
editor.get_path()
for a script existing in Pythonista iCloud.Yes.
editor.get_path()
returns the same string as Python's builtin__file__
but that does not enable scripts in the usual default directory to read from and write to iCloud. -
i never used any ipad i am using laptop