##########################################################
# Demo update and read permissions for Python in Termux.
# All results here assume that termux-setup-storage has 
# been run, and the hosting device has not been rooted.
# These dictate what and where Mergeall can operate:
# update is required to make changes, but not for reads. 
# The /storage/5C32-5336 drive here is a removal SD card,
# /sdcard is internal storage, /data/data is app private.
##########################################################

Welcome to Termux!
...
$ python
Python 3.7.1 (default, Oct 21 2018, 18:20:26)
[Clang 7.0.2 (https://android.googlesource.com/toolchain/clang 003100370607242d on linux
...


#=========================================================
# UPDATES: Termux app-private works, but it's limited
#=========================================================

>>> open('/data/data/com.termux/xxx.txt', 'w').write('1334')
4


#=========================================================
# UPDATES: nested app-specific folders in SD cards work
#=========================================================

>>> open('/storage/5C32-5336/Android/data/com.termux/MY-STUFF/temp.txt', 'w').write('xxxx')
4
>>> open('/storage/5C32-5336/Android/data/com.termux/temp.txt', 'w').write('xxxx')
4


#=========================================================
# UPDATES: all other SD-card paths fail
#=========================================================

>>> open('/storage/5C32-5336/Android/data/temp.txt', 'w').write('xxxx')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 13] Permission denied: '/storage/5C32-5336/Android/data/temp.txt'

>>> open('/storage/5C32-5336/Android/temp.txt', 'w').write('xxxx')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 13] Permission denied: '/storage/5C32-5336/Android/temp.txt'

>>> open('/storage/5C32-5336/temp.txt', 'w').write('xxxx')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 13] Permission denied: '/storage/5C32-5336/temp.txt'


#=========================================================
# READS: but read-only access allowed anywhere on drive
#=========================================================

>>> open('/storage/5C32-5336/existing.txt', 'r').read()
'xxxx'
>>>
>>> open('/storage/5C32-5336/existing.txt', 'w').write('zzzz')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 13] Permission denied: '/storage/5C32-5336/existing.txt'
>>>
>>> open('/storage/5C32-5336/existing.txt', 'r').read()
'xxxx'


#=========================================================
# UPDATES: internal storage works too (by multiple names)
#=========================================================

>>> open('/sdcard/temp.txt', 'w').write('xxxx')
4
>>> open('/storage/emulated/0/temp.txt', 'w').write('xxxx')
4

>>> import os
>>> os.path.realpath('/sdcard/temp.txt')    # they're the same
'/storage/emulated/0/temp.txt'
>>>
>>> os.path.samefile('/sdcard/temp.txt', '/storage/emulated/0/temp.txt')
True

>>> open('/sdcard/temp.txt', 'r').read()
'xxxx'
>>>
>>> open('/sdcard/WEBSITE/temp.txt', 'w').write('zzzz')
4
>>> open('/sdcard/WEBSITE/temp.txt', 'r').read()
'zzzz'
>>>
>>> open('/sdcard/WEBSITE/UNION/about-python.html', 'r').read()[:30]
'<!DOCTYPE HTML PUBLIC "-//W3C/'

# updates work anywhere on internal storage (oddly...)
>>> open('/sdcard/Android/data/com.facebook.katana/test.txt', 'w').write('xxx')
3
>>> open('/sdcard/Android/data/com.facebook.katana/test.txt').read()
'xxx'


#=========================================================
# UPDATES: setting up a work folder on internal storage 
#=========================================================

$ cd ~
$ pwd
/data/data/com.termux/files/home
$ ls /sdcard
360            Movies         Ringtones
Alarms         Music          Samsung
Android        My Documents   WEBSITE
CODE           Notifications  backups
DCIM           Pictures       documents
Download       Playlists      qpython
LazyList       Podcasts       temp.txt
$ mkdir /sdcard/work
$ cd /sdcard/work
$ ls
$ vi test.py
$ ls
test.py
$ cat test.py
open('test.txt', 'w').write('qwerty')
print(open('test.txt').read())
$ python test.py
qwerty
$ ls
test.py   test.txt


#=========================================================
# SUMMARY: UPDATES
#=========================================================


# ROOTS/ARBITRARY: yes internal, no removable

$ python

>>> open('/sdcard/test.txt', 'w').write('xxx')
3
>>> open('/storage/5C32-5336/test.txt', 'w').write('xxx')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 13] Permission denied: '/storage/5C32-5336/test.txt'


# OTHER APP'S FOLDERS: yes internal, no removable

>>> open('/sdcard/Android/data/com.facebook.katana/test.txt', 'w').write('xxx')
3
>>> open('/storage/5C32-5336/Android/data/com.facebook.katana/test.txt', 'w').write('xxx')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 13] Permission denied: '/storage/5C32-5336/Android/data/com.facebook.katana/test.txt'


# OWN APP FOLDER: yes on both

>>> open('/sdcard/Android/data/com.termux/test.txt', 'w').write('xxx')
3
>>> open('/storage/5C32-5336/Android/data/com.termux/test.txt', 'w').write('xxx')
3


# APP-PRIVATE (internal): yes, but hidden to other apps, including file explorers

>>> open('/data/data/com.termux/test.txt', 'w').write('xxx')
3
