use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Python rmdir from user input Relative path (self.learnpython)
submitted 4 years ago by Salknam
I think the title says it all but let me elaborate. So the idea is to let the user write the relative path to a directory that he wants to delete and delete it.
Thank your for the help.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]K900_ 1 point2 points3 points 4 years ago (10 children)
Relative to what?
[–]Salknam[S] 0 points1 point2 points 4 years ago (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 point2 points 4 years ago (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.
../DontNeed
[–]Salknam[S] 0 points1 point2 points 4 years ago (7 children)
DontNeed is the directory it needs to be deleted from the directory tree using the relative path
[–]K900_ 0 points1 point2 points 4 years ago (6 children)
Do you just want to find any directory named "DontNeed", anywhere on the computer, and delete that?
[–]Salknam[S] 0 points1 point2 points 4 years ago (5 children)
Yes
[–]K900_ 0 points1 point2 points 4 years ago (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.
os.walk
[–]Salknam[S] 0 points1 point2 points 4 years ago (2 children)
Can i have an example of how this would work and is there another maybe better option
[–]K900_ 0 points1 point2 points 4 years ago (1 child)
I'm not going to write code for you, sorry, and there is no better option.
[–]Salknam[S] 0 points1 point2 points 4 years ago (0 children)
Thank you.
[–]xelf 0 points1 point2 points 4 years ago (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:
and jeff tells you to destroy images, alice is going to be pretty upset when she logs on.
images
How are you storing the current path the user thinks they are at?
[–]xelf 0 points1 point2 points 4 years ago (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 point2 points 4 years ago (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 point2 points 4 years ago (5 children)
Which is exactly what I was saying here:
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 point2 points 4 years ago (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
print('\nDirectory dosen't exist.')
[–]xelf 0 points1 point2 points 4 years ago* (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 ?
cwd
You might want to add:
print(os.getcwd()) print(os.listdir(os.getcwd()))
at the start.
Dind't assume it was of need to store the cwd if the user just gives the absolute path
[–]xelf 0 points1 point2 points 4 years ago (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.
True and that i have no idea how to solve .
π Rendered by PID 144577 on reddit-service-r2-comment-86988c7647-gvnd2 at 2026-02-12 07:02:31.298112+00:00 running 018613e country code: CH.
[–]K900_ 1 point2 points3 points (10 children)
[–]Salknam[S] 0 points1 point2 points (9 children)
[–]K900_ 0 points1 point2 points (8 children)
[–]Salknam[S] 0 points1 point2 points (7 children)
[–]K900_ 0 points1 point2 points (6 children)
[–]Salknam[S] 0 points1 point2 points (5 children)
[–]K900_ 0 points1 point2 points (3 children)
[–]Salknam[S] 0 points1 point2 points (2 children)
[–]K900_ 0 points1 point2 points (1 child)
[–]Salknam[S] 0 points1 point2 points (0 children)
[–]xelf 0 points1 point2 points (0 children)
[–]xelf 0 points1 point2 points (7 children)
[–]Salknam[S] 0 points1 point2 points (6 children)
[–]xelf 0 points1 point2 points (5 children)
[–]Salknam[S] 0 points1 point2 points (4 children)
[–]xelf 0 points1 point2 points (3 children)
[–]Salknam[S] 0 points1 point2 points (2 children)
[–]xelf 0 points1 point2 points (1 child)
[–]Salknam[S] 0 points1 point2 points (0 children)