r/Python • u/Technical-Ad-565 • 6d ago
Discussion Coding to comparing filenames and foldernames on android
UPDATE: I have temporary solved my immediate problem with using the find and diff commands from Termux.
This got me a usable list of differences. Although I had to move folders around a bit.
In the long run I will use Python for this and further functions.
END UPDATE.
I need to make a program för comparing file names and subfolder names between 2 folders containing something like 100000 files and subfolders. And it's around 650GB of data on the phone.
(Background: I have programmed professionally since 1979, but not on android or pc. Are totally new to Python. I use the phone almost always, very rarely the pc, even for my own business.)
I have searched for and tried extensively with apps for android but there isn't any useful. Apparently mainly due to permission problems.
The main question is: is it at all possible due to the permission problem?
(The solution by transferring the files to the pc have the problem that it takes very long time. And I really want a way to do it relatively often.)
The second question is: are the any existing code (snippets) for this or have you some advices in general?
Thanks on advance for any suggestions.
//Thomas
3
u/k0rvbert 6d ago
Try asking some android devs, there isn't much overlap with python for that platform
0
u/Technical-Ad-565 6d ago
Suggestion for a subreddit for that? (I'm assuming there are several possible.)
2
u/Negative_Pay_2940 6d ago
what phone have 650gb storage
0
u/Technical-Ad-565 6d ago
I have Samsung S25 Ultra with "1TB" storage. In reality though it's about 0.91TB.
2
u/FoeHammer99099 6d ago
If you're able to get a shell on the phone you should be able to run diff -qr dir1/ dir2/
0
u/Technical-Ad-565 6d ago
I tried (both with and whitout double quotes) and got this:
~ $ diff -qr "-Thomas/-B/hist/hist0/- -/-Unsorted/" "-Thomas/-B/hist div all/" diff: invalid option -- 'o' diff: Try 'diff --help' for more information.
I don't understand the reference to 'o' (I'm unfamiliar to the thr general command syntax.)
2
u/brasticstack 6d ago
Try a double dash between
-qrand the paths. I think it's trying to interpret the path names as options.
diff -qr -- '/path1' ..etcEDIT: I also had no idea you could diff files in a path without
lsorfind.1
u/Technical-Ad-565 6d ago
The double dash helped. But it seems it have problem handling hyphens. Will try with some changes in folder names.
2
u/brasticstack 6d ago edited 6d ago
Here's the shell-based method I was thinking of:
``` ls -1 /dir1 | sort > dir1.txt ls -1 /dir2 | sort > dir2.txt cat dir1.txt dir2.txt | uniq -c | egrep -v '\s+1'
should show duplicates between dirs.
```
You can also diff the txt files or whatever other method you want to compare.
I'm not at a computer, so that egrep command might need some tweaking to work right.
EDIT: sort -c not countEDIT2:uniq -cso much for being helpful away from my terminal1
u/FoeHammer99099 6d ago
That's weird. Maybe try with single quotes? It's probably the dashes in the paths being misinterpreted as cli options.
1
u/Technical-Ad-565 6d ago
When I changed all hyphens and blanks in the names to "x" the error message disappeared. (But it of course couldn't find the directories.)
2
u/brasticstack 6d ago
Your phone has Python installed! You could get the Termux app and run it on the terminal.
Use pathlib to locate and compare filenames, then IMO the fastest method would be moving the files you want to transfer to their own directory, making a tarball of that dir if space permits and copying that. Otherwise it's very slow doing files individually through ADB (single threaded) or MTP (dog slow for some reason.)
2
u/Glittering_Box5197 6d ago
Yes, it is possible in Python, but on Android the permission/access method is likely to be the harder part, especially for 650 GB of data.
If the two folders are accessible to Python, you don't need to read the contents of all 650 GB just to compare filenames and folder names. You can compare the directory trees by walking the two folders and building relative paths.
For example, the basic approach is:
from pathlib import Path
def get_paths(folder):
return {
p.relative_to(folder)
for p in Path(folder).rglob("*")
}
folder1 = Path("/path/to/folder1")
folder2 = Path("/path/to/folder2")
paths1 = get_paths(folder1)
paths2 = get_paths(folder2)
only_in_1 = paths1 - paths2
only_in_2 = paths2 - paths1
print("Only in folder 1:", len(only_in_1))
print("Only in folder 2:", len(only_in_2))
This compares the relative filenames and subfolder structure, rather than comparing the actual file contents.
With around 100,000 entries, I'd also avoid loading unnecessary file metadata or file contents. If you eventually want to compare whether files themselves are identical, that's a different problem because you'd need to consider size, modification time, hashes, etc.
On Android, I'd first solve the access problem with a small test program that can successfully list the contents of both folders. Once Python can see both directory trees, the comparison itself is fairly straightforward.
Since you're new to Python, I'd start with a program that simply prints the number of files/folders found in each directory before attempting the full comparison.
1
u/Technical-Ad-565 5d ago
Thanks for the code example. My first concern will be the permission problem. A test program is of course the first thing I must do. But if someone has tried this and it worked would be helpful to know. Due to lack of time I haven't began with this "project" so I just took on the comparison problem. (My main objective is to be able to handle and register connections between files and folders and "tag" them depending on content and relationship with other files etc.)
2
6d ago
[removed] — view removed comment
1
u/Technical-Ad-565 5d ago edited 5d ago
I actually just installed and used Termux. Used find and diff commands as a temporary stop gap until another/better solution.
As you mentioned my basic concerns is the permission problems. It seems all commercial or easy availible apps is blocked by these (for my needs).
- The question I have do these permission problems also block my Python programs? *
(Thanks for the code suggestions! If I can go the Python way I will create extensive lists in files about files and folders. I have more needs than I mentioned in my post.)
2
5d ago
[removed] — view removed comment
1
u/Technical-Ad-565 5d ago
Thanks for the very valuable info!
(I have no need to access the app data or system folders and files. I don't like to shot me in the foot. :D )
2
u/11krish11 4d ago
Yes, it is definitely possible directly on Android using Termux (a Linux terminal environment app available on F-Droid or GitHub).
- Grant Storage Permissions: After installing Termux, run
termux-setup-storageand allow "All files access" in Android Settings so scripts can scan your internal storage/SD card without permission blocks. - Efficient Python Approach: For 100,000+ files, avoid reading file contents. Collect relative paths into Python
setstructures usingos.scandir()oros.walk(), then use set operations: - import os
- def get_rel_paths(root):
- paths = set()
- for dirpath, dirnames, filenames in os.scandir(root):
- # build relative path set for quick difference lookup
- ...
Fast Comparison: Computing folder_a - folder_b using sets is an $O(n)$ in-memory operation that completes in seconds once the path tree is indexed.
1
u/Technical-Ad-565 4d ago
Thanks. I will (almost) never read file contents. The intended functionality is solely focused on file names, in wich folders they are and the same for folders themselves. Add to that "tagging" all those in lists/crosslists in files. The tagging could be anything descriptive. (Here we are talking about tags that cannot solely be a subordinate to another tag; some simple examples: time, places, people and things.)
2
u/11krish11 4d ago
Since you're dealing with multi-attribute tagging across 100,000+ files, using an embedded database like SQLite (built right into Python) will be far more manageable and faster than raw text lists.
- Database schema: Store file paths in one table and tags in another, linked by a standard many-to-many relationship (or use SQLite JSON / comma-separated tags if keeping it simple).
- Multi-criteria filtering: With SQLite, querying files by complex tag combinations (e.g.,
WHERE tag IN ('place_A', 'person_B')) is nearly instantaneous even on a phone.- Persistent & portable: The entire index and tag metadata live in a single
.dbfile that you can easily back up or inspect without scanning the filesystem every time.If you prefer a pure Python data structure to start, a
dictmapping relative paths to sets of tags ({ "subfolder/file.ext": {"place", "2024", "person"} }) dumped to a JSON file will also work cleanly.1
1
u/snugar_i 5d ago
Why Python? It's not really that common on Android
1
u/Technical-Ad-565 5d ago
Just because I like it. Based on the overall functionality and that it don't overcomplicate things. Which I like as a programmer. (My code will never be used by other people.)
-1
u/Turtlestacker 6d ago
Im guessing you havent tried codex / Claude?
0
u/Technical-Ad-565 6d ago
I'm sorry but I don't understand. Are you e g suggesting I use AI? (Haven't used it.)
1
2
u/Bright-Historian-216 6d ago
android is based on the linux kernel. i'm not familiar with android specifically, but you may be able to install python like you would on linux and use it in the same way. though, this may or may not require you to root your phone.