Search code examples
iosfilesystemspythonista

Where to save text file on iPad


I have finished writing a program on Windows laptop. It is a program that picks random words from a text file. On Windows OS, I saved the .txt file in the same directory as the .pyw and everything is fine.

Then I uploaded the .pyw and .txt on google drive and opened them on my iPad with pythonista. When I run the program, it says ‘No such file or directory’. (see screenshot below)

screenshot of the code and error message

I just got my iPad pro about a week ago and I am still not sure how the directories work on iOS. It is not very intuitive without a file explorer. I just know that in iOS you are supposed to open a file with certain apps and it is saved it its own ‘library’?

Can anyone tell me what I am doing wrong? Perhaps a brief background on how iOS file system works could help me understand.

I resorted to asking what may seem like a relatively ‘simple’ question here because I am self-learning. Also, in South Korea (which is where I am) iOS developers are very rare to come by. Everyone does windows or android but not iOS. It is a very difficult for me to find any useful information regarding compatibilty issues that I run into going from PC to iOS.

Any help would be much appreciated.


Solution

  • According to your screenshot, the file is called “wordlist.txt”, and not “wordlist”, which is what your script is trying to open, though it does look like you tried “wordlist.txt” at one point as well.

    1. You can tap those diagonal arrows in the error message to get a more verbose error message. This will also allow you to print a standard Python traceback to the console. You can also look at the value of your variables.

    2. If you’re running this script by hand, you should be in the correct directory and so be able to open “wordlist.txt” in the manner you’re doing it in the script (assuming you use the correct filename, of course). However, it’s a good idea to make sure you’re in the correct directory when using Pythonista, as the working directory can vary depending on how you open the script. Preface your script with os.chdir(os.path.dirname(__file__)) to ensure that your working directory is the same as the directory that your script is in. You’ll also have to import os.

    The iOS file system, as far as you’re concerned in Pythonista, is the same as any Linux/Unix file system. The os module in Python will work the same as elsewhere.

    If this doesn’t help, change the open line to f = open("wordlist.txt") and then access the more detailed error message/traceback to see what the full error is.

    It will probably also help to test with a slimmed down version of the script that lets you focus on where you’re having a problem. Something like:

    import os
    os.chdir(os.path.dirname(__file__))
    
    f = open("wordlist.txt")
    total = f.readlines()
    f.close()
    
    print(total)