#πŸ”’ My delete function doesn't work

153 messages Β· Page 1 of 1 (latest)

fathom zenith
vague craneBOT
#

@fathom zenith

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

celest gorge
#

what function specifically fails, in what file? Could you be a bit more precise

tiny turret
kind ledge
#

generate hashes of both files and compare those

#

you can do this piece by piece

fathom zenith
fathom zenith
kind ledge
#

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?

fathom zenith
#

isn't filechk enough? it's meant for checking in 2 folders you know have some files that are the same

kind ledge
#

and how do you suppose that works

#

if not the way I explained

fathom zenith
#

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

kind ledge
#

at this point I'm not sure what you're asking for

fathom zenith
#

i need help with my delete function

#

it's in my gitlab

kind ledge
#

do I need to search for it or will you point it out for me?

fathom zenith
#

it fails to delete files saying they do not exist

fathom zenith
kind ledge
#

that was a generic link to your repo

#

not a link to a specific file or line

kind ledge
#

what is if __name__ == "__main__": doing on line 20?

#

that is a really strange place for such a check

fathom zenith
#

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

kind ledge
#

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

kind ledge
#

so how about you print the paths to be deleted and go check if the files really are there, yes?

tiny turret
fathom zenith
fathom zenith
#

just read the script

#

they exist i checked

kind ledge
#

are the paths absolute or relative?

fathom zenith
#

what do you mean?

kind ledge
#

absolute paths mean like C:\foo\bar <- they contain the absolute path from the file system root

fathom zenith
#

absolute then

kind ledge
#

relative paths don't start with a drive letter or slash

fathom zenith
#

but it's on linux so no C:/

kind ledge
#

I need to go, maybe someone can take it from here?

fathom zenith
#

ok

#

bye

tiny turret
fathom zenith
#

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?

tiny turret
#

you don't need the os modules, you should be using Path throughout
and it would be better to not use global variables

tiny turret
fathom zenith
#

can you help me?

#

i want to fix this for 2.0

#

but also i need the delete function

tiny turret
#

in a while, i'm on the move right now

fathom zenith
#

you said something about path instead of os

fathom zenith
#

thanks for your help

#

i will try to do the things you told me

tiny turret
#

how have your code progressed?

fathom zenith
#

I didn't change anything yet

tiny turret
#

@fathom zenith do you have time and are you willing to work on your code right now?

vocal bloom
vague craneBOT
#

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...
vocal bloom
#

explicitly pass shallow=False and it will compare the file contents.

tiny turret
#

yeah, it's the default shallow setting that is the problem for OPs usage, but without it the code will be really slow

kind ledge
#

I didn't know about this module!

tiny turret
#

this is what i meant with them either not using it correctly or for the correct purpose earlier

fathom zenith
tiny turret
fathom zenith
fathom zenith
tiny turret
fathom zenith
#

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

tiny turret
fathom zenith
vocal bloom
#

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.

vague craneBOT
#

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')
```...
tiny turret
#

!doc pathlib.Path.unlink

vague craneBOT
#

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)...
tiny turret
#

you probably want to set missing_ok=True as well

fathom zenith
vocal bloom
#

no, that only duplicates filecmp's logic.

tiny turret
#

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?

vocal bloom
#

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.)

fathom zenith
vocal bloom
#

well, you know what a dict is and how to manipulate them?

fathom zenith
vocal bloom
#

!doc dict

vague craneBOT
#

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...
vocal bloom
#

hmm that might not be the most useful

tiny turret
vocal bloom
#

is there a command for links to tutorial sections?

tiny turret
fathom zenith
vocal bloom
#

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:

  1. check the size
  2. look up the previous file paths with the same size
  3. If there were none, this file is (so far) unique; store a new list in the dictionary with just that path
  4. Otherwise, loop over those other paths and do the hard comparison work
  5. If the new file was (so far) unique, add that path to the list. otherwise, delete the file
fathom zenith
vocal bloom
tiny turret
fathom zenith
tiny turret
fathom zenith
#

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

tiny turret
# fathom zenith What changed?

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
vocal bloom
fathom zenith
#

I rushed writing it on mobile and messed up

#

I didn't mean to say "You read what you wrote".

tiny turret
fathom zenith
#

I don't fully do everything with ai

vocal bloom
#

I know it was "I"

fathom zenith
vocal bloom
#

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

tiny turret
vocal bloom
#

it's fine, I'm used to typos

#

(and I've also dealt with much worse English. You're doing well, really.)

tiny turret
#

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)

fathom zenith
#

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?

tiny turret
#

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

fathom zenith
tiny turret
#

because you will need to process each list that contains more than one item

fathom zenith
#

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.

tiny turret
fathom zenith
#

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

vague craneBOT
#
Python help channel closed for inactivity

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.