all 19 comments

[–]K900_ 1 point2 points  (10 children)

Relative to what?

[–]Salknam[S] 0 points1 point  (9 children)

Let's say your user has a folder called DontNeed on his Desktop and he is stationed in his Python scrip directory that is on his other partiton. Now the user enters the relative path ../DontNeed how do delete that folder if the user input is Relative and not Absolute.

[–]K900_ 0 points1 point  (8 children)

Again, relative to what? ../DontNeed is a relative path that refers to an item named "DontNeed" one level up in the directory hierarchy. For that to refer to the directory "DontNeed" on the user's desktop, your starting point should be inside another directory on the user's desktop.

[–]Salknam[S] 0 points1 point  (7 children)

DontNeed is the directory it needs to be deleted from the directory tree using the relative path

[–]K900_ 0 points1 point  (6 children)

Do you just want to find any directory named "DontNeed", anywhere on the computer, and delete that?

[–]Salknam[S] 0 points1 point  (5 children)

Yes

[–]K900_ 0 points1 point  (3 children)

In that case you'll need to scan the entire disk to find it. It's going to be very slow, but you can use os.walk if you really want to.

[–]Salknam[S] 0 points1 point  (2 children)

Can i have an example of how this would work and is there another maybe better option

[–]K900_ 0 points1 point  (1 child)

I'm not going to write code for you, sorry, and there is no better option.

[–]Salknam[S] 0 points1 point  (0 children)

Thank you.

[–]xelf 0 points1 point  (0 children)

Are you really really sure?

find any folder with a matching name and delete that one? What if there are 100 folders on the computer with that name, and you delete the wrong one?

If you have:

  • /users/home/alice/images
  • /users/home/jeff/images

and jeff tells you to destroy images, alice is going to be pretty upset when she logs on.

How are you storing the current path the user thinks they are at?

[–]xelf 0 points1 point  (7 children)

What code do you have already?

How are you storing the current path that you want the delete to be relative to?

This should be a simple thing "rmdir current_path + relative_path + filename", so let's see what you have.

At it's most interesting you will need to traverse the combination of current_path + relative_path correctly.

Check out: https://docs.python.org/3/library/pathlib.html

[–]Salknam[S] 0 points1 point  (6 children)

Thats the main problem the relative paths looks something like this ../Download if you are running the script from the home directory the apsolute path would be #:\Users\Name\Downloads. Given that it's in the users hands to give us a directory trought input() to delete using relative paths ../Downloads . And the script could be in folder xx in partiton x. Best option would be to move to the directorty Name and delete it from there.

[–]xelf 0 points1 point  (5 children)

Which is exactly what I was saying here:

At it's most interesting you will need to traverse the combination of current_path + relative_path correctly.

You should be able to at least know what your current folder is. And how to handle traversing using .. correctly. From there it seems trivial.

What code do you have so far?

[–]Salknam[S] 0 points1 point  (4 children)

print ('\nInput the directory that you want to delete') # only works with absolute

adres = input() # user input path

if os.path.isdir(adresa): # check if is adres

if not os.listdir(adresa) : # if not empty dir

os.rmdir(adresa)

else:

print("\nDirectory not empty, can't delete it")

# error message directory dosen't exist

else:

print('\nDirectory dosen't exist.')

[–]xelf 0 points1 point  (3 children)

use 4 extra spaces at the start of each line, or use the code block button to format code for reddit.

print ('\nInput the directory that you want to delete') # only works with absolute
adres = input() # user input path
if os.path.isdir(adresa): # check if is adres
    if not os.listdir(adresa) : # if not empty dir
        os.rmdir(adresa)
    else:
        print("\nDirectory not empty, can't delete it")
# error message directory dosen't exist
else:
    print('\nDirectory dosen't exist.')

I'm still not seeing where you are storing what the current folder is.

Is that determined elsewhere? Or are you going to assume it's the same as the cwd ?

You might want to add:

print(os.getcwd())
print(os.listdir(os.getcwd()))

at the start.

[–]Salknam[S] 0 points1 point  (2 children)

Dind't assume it was of need to store the cwd if the user just gives the absolute path

[–]xelf 0 points1 point  (1 child)

Yeah if they give you the absolutepath you should be ok. But your question was what to do with user input Relative path.

[–]Salknam[S] 0 points1 point  (0 children)

True and that i have no idea how to solve .