File Browser question
omz last edited by
@Matteo Here's a simple method to open a folder in the file browser programmatically:
import webbrowser webbrowser.open('pythonista3://Examples')
If your folder name includes characters that aren't allowed in a URL (e.g. spaces), you'll have to percent-encode them using
urllib.parse.quote(). If your folder is in iCloud, append
?root=icloudat the end of the URL.
@Matteo You're right, I'm in Pythonista V3.2, sorry. Hoping the answer of omz will help you.
@omz Hi thank you for your help, interesting use of webbrowser, unfortunately when I run the script it opens the Example folder but it is empty, no files inside, but I know that inside that folder I find all Pythonista built-in example scripts. I run your script with python 3.5.1.
Have you some ideas about this?
@Matteo Strange, with Pythonista 3.2, all is ok
@cvp So it is a feature of the new version 3.2, the 3.1 version has not this feature. Is it right?
As I do not want to update Pythonista for now, do you know some workaround using obj-c?
Note that I use Pythonista 3.1
@cvp Ok no problem, I was hoping my request was easy and available in version 3.1.
To upgrade to version 3.2 I'd like to know the name and version of each preinstalled library in 3.2, compared to 3.1. Sorry for this, but usually I don't upgrade any software/app if I don't know precisely what I will find in new version compared to old version. Even more if the app that I update is not uninstallable and replaceable with the old version, if I want to go back to the old version.
@Matteo Try, it's ok for me in V3.2
import webbrowser webbrowser.open('pythonista3://Examples/.')
Hi @cvp, thank you but it doesn't work in v3.1. Maybe no solution with 3.1.
@cvp No problem cvp, thank you anyway 😉😊
Matteo last edited by Matteo
Hi, I was curious about the dot in your code and I ask if you know other symbols that have some similar effect, for example the symbol * (I don’t know if it exists in url scheme of Pythonista) for opening all files in a folder. The code could be:
import webbrowser webbrowser.open('pythonista://my-folder/*')
and this script could open all files .py inside my-folder (as result user will see each new tab for each file opened in the Pythonista text editor).
So do you know some other symbols and their effect in Pythonista url scheme like the dot
I ask this because I discovered, based on your post, that if I have the following script in a folder named for example
'Documents/my-folder_01/my-folder_11/my-folder_12/'('Documents' is the main folder of Pythonista, where user finds built-in folders like 'Examples' or 'Modules & Templates'):
import webbrowser webbrowser.open('pythonista://.')
and I run this script, Pythonista v3.1 opens file browser and goes in the path where this script is saved.
Based on this, I tried to write this piece of code (script 'A'):
import webbrowser import os folder_path = os.path.expanduser('~/Documents/my-folder_01/my-folder_11/my-folder_12/') content_file = ''' import webbrowser webbrowser.open('pythonista://.') ''' with open(folder_path + '.folder.py', 'w') as f: f.write(content_file) webbrowser.open('pythonista://my-folder_01/my-folder_11/my-folder_12/.folder.py?action=run')
I saved this script in a folder named ‘Documents/Tests/’ (as example).
I tried to run it and magically (at least for me) Pythonista v3.1 opens instantly the folder
'Documents/my-folder_01/my-folder_11/my-folder_12/'with its built-in file browser, allowing me to perform all the operation on files I need without searching manually that folder touching several times the screen of my iphone. To perform operation on files in the folder I use the JonB's user_key_bar script that allows user to create own keys in the top bar of Pythonista.
Obviously this method is good for me only if the folder doesn’t change, so when I don’t need to change path of 'folder_path' in script 'A'. But for now I need to perform the same file operations always in a constant folder.
I know I could write a script that perform all this automatically, but I prefer to use file browser and JonB user_key_bar with my own actions because it is faster for me (I'm trying to use Pythonista also like a powerful programmable file browser, like a quite full and complete OS inside the quite closed and restricted iOS of iphone).
About what I discovered I ask:
- How can user show in Pythonista file browser all hidden file starting with a dot , like '.folder.py' in script 'A'? Maybe with an option in settings? Or a piece of code?
- How can user open a file (or several files at once) from file browser or from home screen of idevice without closing the current opened file in the active tab? I noticed that when I open a script, Pythonista opens it for editing closing the existing active tab (so no tab replacing but tab adding).
- How user can open and close tabs in editor programmatically (which commands)?
- How can user programmatically delete hidden files after the last line of code in script 'A' (in the example the hidden file is '.folder.py') inside the same script 'A'?
Note that I still use Pythonista v3.1.
@Matteo Every directory contains a . and a .. entry. (in Unix, Windows, etc...)
. means the directory itself. It is called the current directory.
.. means the directory's parent directory, that is, the directory that contains it.
@Matteo to delete the file at end of your script
Import os os.remove(folder_path+'.folder.py')
@Matteo I think that it is not "magically". When you run a script, Pythonista opens its folder, thus...
Matteo last edited by Matteo
@cvp Hi , thank you for reply: 'magically' because in previous posts we read that omz solution works only in version 3.2, when I run it in v3.1 I obtain a blank folder, even if I'm sure I have some files inside it.
After my last post I can open any folder programmatically also in v3.1 (it is not elegant because the solution I found needs to create a new file inside the folder where I want to go with file browser and in order to not see it I must hide it with a dot at the beginning of filename).
Thank you also for deleting hint, unfortunately when I add:
import os os.remove(folder_path + '.folder.py')
of script 'A', the script doesn't work as expected, because it deletes the file '.folder.py' even before that the istruction
Have you some other ideas about deleting a file named 'B' after a webbrowser.open use that needs the file 'B'?
@Matteo The script of omz did open a folder, your script runs a script, that makes the magic difference.
Put a little sleep after webbrowser.open to let it time to perform its action before the remove
import time time.sleep(0.2)
@cvp It works! Thank you, sometimes sleeping is good, also for a code ;-)
@Matteo Surely when the work is done during your sleep 😂