@djorge You'll find hereafter my cleaned "import any file", it uses a File_Picker module of omz, you can find it here
# coding: utf-8
import appex
import console
import shutil
import os
import ui
import File_Picker
import unicodedata
import urllib.request, urllib.parse, urllib.error
def process():
global my_back
# called by sharing action or launcher
if not appex.is_running_extension():
return
# Sharing: receive file to store in local folder
fil = appex.get_file_path()
if fil == None:
# no file shared
url = appex.get_url()
fil = urllib.request.urlopen(url)
tot = fil.getheader('content-length')
i = url.rfind('/') # index last /
nam = url[i+1:] # right of last /
block = 4096*1#6
temp = os.path.join(os.path.expanduser("~"),'Documents/'+nam)
with open(temp, 'wb') as output:
while True:
data = fil.read(block)
if data:
output.write(data)
else:
break # leave loop
fil = temp
local_file = nam
else:
# file shared
temp = None
local_file = os.path.basename(fil)
# Select folder where to move the file
to_dir = File_Picker.file_picker_dialog('select folder where to copy', multiple=False, select_dirs=True, file_pattern=r'^.*\.py$')
if to_dir != None:
w = '/Pythonista3/Documents/'
i = to_dir.find(w)
dir_loc = to_dir
if i >= 0:
dir_loc = to_dir[i+len(w):]
else:
dir_loc = ''
else:
if temp:
os.remove(temp)
return
# Remove accents of file name
local_file = ''.join((c for c in unicodedata.normalize('NFD', local_file) if unicodedata.category(c) != 'Mn'))
try:
local_file = console.input_alert('Import file\n'+fil+'\nin', dir_loc, local_file,'ok')
import_file = True
except KeyboardInterrupt:
import_file = False
if not import_file:
if temp:
os.remove(temp)
return
local_path = os.path.join(os.path.expanduser("~"),'Documents/'+dir_loc)
local_path = os.path.join(local_path,local_file)
shutil.copy(fil,local_path)
if temp:
os.remove(temp)
def main():
global my_back
#----- Main code ------------------------
console.clear()
# Hide script
#w, h = (540,620)
my_back = ui.View(name='Import file')
my_back.background_color='white'
my_back.present('sheet',hide_title_bar=True)
process()
my_back.close()
appex.finish()
if __name__ == '__main__':
main()