https://gitlab.com/super.2061/photo-organizer
My app says the file i'm trying to delete doesn't exist when it clearly indexed it and i can see it in my file manager too. Can you help me fix this?
153 messages Β· Page 1 of 1 (latest)
https://gitlab.com/super.2061/photo-organizer
My app says the file i'm trying to delete doesn't exist when it clearly indexed it and i can see it in my file manager too. Can you help me fix this?
@fathom zenith
Remember to:
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
what function specifically fails, in what file? Could you be a bit more precise
first of all: your code does not properly check for duplicate files, if to files happen to have the same size and timestamp they will be reported as duplicates without checking the content to see if they really are duplicates or not
any idea how to fix that?
when i run the script and click delete files it says file not found. the delete function delete_doubles()
wouldn't this take too long? also whats a way to do that?
or even simpler, just read fixed-sized blocks of both files and compare
well how else would you determine if a single bit in a random location was different?
isn't filechk enough? it's meant for checking in 2 folders you know have some files that are the same
idk. it worked in all my tests. it works like you aid but isn't this enough?
or else why does the library even exist?
also i can fix this issue but first i need to fix files not being able to get deleted
at this point I'm not sure what you're asking for
do I need to search for it or will you point it out for me?
it fails to delete files saying they do not exist
it was in the top message of this thread
what is if __name__ == "__main__": doing on line 20?
that is a really strange place for such a check
i need it trust me
i import the functions of this file to the gui version as a library and without it the cli version of functions run
so i thought of this
this check is in the wrong place, just saying
but anyway
you have a problem with deletion because the files to be deleted supposedly don't exist, right?
or at least you get an error to that effect
yes
so how about you print the paths to be deleted and go check if the files really are there, yes?
you're just not using it correctly or for the right purpose
they exist and the scan function finds them but the delete function says they don't exist
i added this day 1
just read the script
they exist i checked
are the paths absolute or relative?
what do you mean?
absolute paths mean like C:\foo\bar <- they contain the absolute path from the file system root
absolute then
relative paths don't start with a drive letter or slash
but it's on linux so no C:/
I need to go, maybe someone can take it from here?
then they would start with a / is they are absolute or start with anything else if they are relative
yes they do
it's abolute
can you help me fix the error please? also should i change filechk to something else or its fine and the chance of messing up files is very low?
you don't need the os modules, you should be using Path throughout
and it would be better to not use global variables
it's up to you if you want to take your chances with your files, I wouldn't
and hope no one else that doesn't know about that risk runs the code and maybe delete something important to them
in a while, i'm on the move right now
you said something about path instead of os
ok
thanks for your help
i will try to do the things you told me
okay, now i'm in front of a computer
how have your code progressed?
I didn't change anything yet
@fathom zenith do you have time and are you willing to work on your code right now?
!doc filecmp.cmp
filecmp.cmp(f1, f2, shallow=True)```
Compare the files named *f1* and *f2*, returning `True` if they seem equal, `False` otherwise.
If *shallow* is true and the [`os.stat()`](https://docs.python.org/3/library/os.html#os.stat) signatures (file type, size, and modification time) of both files are identical, the files are taken to be equal.
Otherwise, the files are treated as different if their sizes or contents differ...
explicitly pass shallow=False and it will compare the file contents.
yeah, it's the default shallow setting that is the problem for OPs usage, but without it the code will be really slow
I didn't know about this module!
this is what i meant with them either not using it correctly or for the correct purpose earlier
Very little cause I need to sleep.
ah, then you should priorities that, sleep is important, this can wait for another day
Is this enough and all I need to make it never mess up with files?
If you can say some stuff and I will try implementing it tomorrow morning
it should be enough to not delete files that are different even if the size and timestamp is the same
What about the delete issue where it says file is not found?
Do you have any idea about that? I will also test it on windows tomorrow to see if its an issue with the Linux filesystem
work with Path objects throughout your code
I only know how to use path to get the scripts location. Can you tell me how else I can use it?
As rndpkt says, it will be slow. Right now you attempt to compare every file to every other file (O(n^2)).
Although it's not as bad as it sounds. filecmp.cmp will still do the basic check for file size before it looks at the file contents. But doing this O(n^2) times still adds up. A properly done hash strategy makes it possible for the program to not even have to look at file sizes for each "other" file; it can directly see which files could possibly be a match, and skip the loop.
!doc pathlib.Path
class pathlib.Path(*pathsegments)```
A subclass of [`PurePath`](https://docs.python.org/3/library/pathlib.html#pathlib.PurePath), this class represents concrete paths of the systemβs path flavour (instantiating it creates either a [`PosixPath`](https://docs.python.org/3/library/pathlib.html#pathlib.PosixPath) or a [`WindowsPath`](https://docs.python.org/3/library/pathlib.html#pathlib.WindowsPath)):
```py
>>> Path('setup.py')
PosixPath('setup.py')
```...
use the unlink method
!doc pathlib.Path.unlink
Path.unlink(missing_ok=False)```
Remove this file or symbolic link. If the path points to a directory, use [`Path.rmdir()`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.rmdir) instead.
If *missing\_ok* is false (the default), [`FileNotFoundError`](https://docs.python.org/3/library/exceptions.html#FileNotFoundError) is raised if the path does not exist.
If *missing\_ok* is true, [`FileNotFoundError`](https://docs.python.org/3/library/exceptions.html#FileNotFoundError) exceptions will be ignored (same behavior as the POSIX `rm -f` command)...
you probably want to set missing_ok=True as well
I have an idea though it could also be slow. What if I compared without shallow=True first and then compare the contents only from files the filechk thinks are s
The same?
no, that only duplicates filecmp's logic.
this code does absolutly nothing
def open_folder_dialog():
global image_directory
global image_directory_for_scan
maybe it's incomplete code for a function you are not done implementing yet?
What you really want to do is have a dict mapping from file sizes to file paths with those sizes. Then as you get each new file, you can look up the list of previous files with those sizes, and you only have to loop over that list. Similarly, if you hash the file contents while reading/comparing them, you can add that information to the dict key (or make a separate dict, or a nested dict, etc.)
I don't remember exactly why I did that but I have added many stuff to work with the GUI app since I imported the CLI version as a library and I tweaked many stuff to make it work correctly in both versions
How can I do this?
well, you know what a dict is and how to manipulate them?
No or I dont remember
!doc dict
class dict(**kwargs)``````py
class dict(mapping, /, **kwargs)``````py
class dict(iterable, /, **kwargs)```
Return a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments.
Dictionaries can be created by several means...
hmm that might not be the most useful
here you are using the Path class correctly up until you do str() around path when appending it to the list
def get_photos(dir_path):
photos = []
for path in Path(dir_path).rglob("*"):
if path.is_file() and path.suffix.lower() in {".jpg", ".jpeg", ".png", ".gif", ".webp", ".mp4"}:
photos.append(str(path))
return photos
is there a command for links to tutorial sections?
you should really separate any user interface (GUI and/or CLI) from the code that actually does the important stuff/logic
Why? What is wrong? Also part was wrote with ai assistance cause I didn't know how to sort the files with the extensions
the idea is: if we have for example the file size (an integer) as the key, and a list of file paths as the value, then for each file we can:
OK. I will remember this for new projects but is it causing any issue here?
well, this plus the AI assistance are causing the problem that you don't fully understand the code that you have so far. This makes it harder to change the code with your own intent.
if you instead do
def get_photos(dir_path):
photos = []
for path in Path(dir_path).rglob("*"):
if path.is_file() and path.suffix.lower() in {".jpg", ".jpeg", ".png", ".gif", ".webp", ".mp4"}:
photos.append(path)
return photos
```you can work with the `Path` object for each `file` directly in the rest of the code after that
What changed?
I read what you wrote. What's a way to implement this?
yeah, i kind of suspected this was ai generated when compared to the rest of the code
BTW I don't use AI too much but sometimes I don't know stuff and I use it to learn
I read everything it writes first to understand the logic
i changed
def get_photos(dir_path):
photos = []
for path in Path(dir_path).rglob("*"):
if path.is_file() and path.suffix.lower() in {".jpg", ".jpeg", ".png", ".gif", ".webp", ".mp4"}:
- photos.append(str(path))
+ photos.append(path)
return photos
respectfully, if this is your response to a list of steps then your use of the AI is not teaching you what you think it is
Ok
Sorry I mean "I" not "U"
I rushed writing it on mobile and messed up
I didn't mean to say "You read what you wrote".
you won't really learn from ai when you let ai solve the problem for you
it will most often just lead to the the illusion of knowledge/learning
Also I just don't know perfect English and I also don't have much experience. I just finished middle school. I am the best in the class maybe in the school at coding but its nothing so i am trying to learn more.
I don't fully do everything with ai
I know it was "I"
Oh OK. I got skated I sounded disrespectful even though I really respect you for helping me
the point is, you should be building those skills, but right now the AI is giving you code way ahead of what you actually understand
if you just finished middle school and english isn't your native language i think you are doing great and also doing pretty good with learning programming pretty early, keep at it when it comes to both areas π
it's fine, I'm used to typos
(and I've also dealt with much worse English. You're doing well, really.)
Thanks
Thanks
by the way english isn't my native language either, but i have had quite a few more years than you to learn (even though my english isn't at a native speaker level and probably never will be)
I will read this tomorrow and try to implement what you said
If I get it write do you mean I should first sort by dates and size and stuff and if I find similar ones then run filechk with shallow set to False?
skip dates or timestamps entirely, just check size of each file (after filtering them as you already do with .is_file() and the file extension)
then use the size as the key in a dictionary (and where the value is a list) and append the current file object to the list for that key
that is only the first step in finding duplicate files
OK though I will need to learn how python dictionaries work first
because you will need to process each list that contains more than one item
yes, that is true
OK. I will try to fix the error tomorrow the way you told me and then I will learn how dictionaries work if I have time left cause I also have to study for final exams for next week.
then sleep and study should be on the top of your list
you can continue this after you are finished with your final exams
Nah I will probably find some time. I'm just saying cause I don't know how long it will take to learn the dictionaries. Goodnight
good night and take care
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.