omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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

    Pythonista
    5
    18
    129
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      SpideryArticle last edited by

      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

      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @SpideryArticle last edited by JonB

        @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.

        ccc 1 Reply Last reply Reply Quote 0
        • ccc
          ccc @JonB last edited by

          from pathlib import Path
          
          Path('SpideryArticle.txt').write_text('SpideryArticle was here!')
          
          S 1 Reply Last reply Reply Quote 0
          • S
            SpideryArticle @ccc last edited by SpideryArticle

            @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.

            cvp 1 Reply Last reply Reply Quote 0
            • cvp
              cvp @SpideryArticle last edited by

              @SpideryArticle The created file is a local Pythonista file and these files are not visible in the Files app.

              S 1 Reply Last reply Reply Quote 0
              • S
                SpideryArticle @cvp last edited by

                @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?

                cvp 1 Reply Last reply Reply Quote 0
                • cvp
                  cvp @SpideryArticle last edited by cvp

                  @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

                  see here

                  JonB S 2 Replies Last reply Reply Quote 0
                  • JonB
                    JonB @cvp last edited by

                    @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.

                    S cvp 2 Replies Last reply Reply Quote 0
                    • S
                      SpideryArticle @cvp last edited by ccc

                      @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'
                      
                      cvp 1 Reply Last reply Reply Quote 0
                      • S
                        SpideryArticle @JonB last edited by

                        @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.

                        1 Reply Last reply Reply Quote 0
                        • cvp
                          cvp @JonB last edited by

                          @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

                          1 Reply Last reply Reply Quote 0
                          • cvp
                            cvp @SpideryArticle last edited by cvp

                            @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

                            see here

                            see here

                            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!")
                            
                            ccc 1 Reply Last reply Reply Quote 1
                            • ccc
                              ccc @cvp last edited by ccc

                              # 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.

                              cvp 1 Reply Last reply Reply Quote 0
                              • cvp
                                cvp @ccc last edited by

                                @ccc we can always use editor.get_path() for a script existing in Pythonista iCloud.

                                S ccc 2 Replies Last reply Reply Quote 1
                                • S
                                  SpideryArticle @cvp last edited by

                                  @cvp @ccc @JonB I am sorted now.
                                  Thank you all very much for your collaboration in helping me I really appreciate it.

                                  cvp 1 Reply Last reply Reply Quote 1
                                  • cvp
                                    cvp @SpideryArticle last edited by

                                    @SpideryArticle 👍and welcome here.

                                    1 Reply Last reply Reply Quote 0
                                    • ccc
                                      ccc @cvp last edited by

                                      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.

                                      1 Reply Last reply Reply Quote 0
                                      • I
                                        imran23 last edited by

                                        i never used any ipad i am using laptop

                                        1 Reply Last reply Reply Quote 0
                                        • First post
                                          Last post
                                        Powered by NodeBB Forums | Contributors