Python Forum
only print the last folder ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
only print the last folder ?
#1
hi, I know I can get the name of the last folder using os.path.basename and os.path.normpath.

src_1 = "/storage/emulated/0/Documents/TAX/December_2021"
src_2 = "/storage/emulated/0/Documents/TAX/March_2022"
print(os.path.basename(os.path.normpath(src_1)))

gives me: December_2021

Nice.

HOWEVER, this doesn't work, if I use a "wildcard" as the last folder:
src = "/storage/emulated/0/Documents/TAX/{name of this folder}"

print(os.path.basename(os.path.normpath(src)))

I only get {name of this folder} and not December_2021 or March_2022.

Is there a way to do it this way or do I need another approach?
Thanks for any help!
Gribouillis write May-21-2022, 09:06 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
What do you mean with wildcard?

and why

print(os.path.basename(os.path.normpath(src_1)))

That's enough

print(os.path.basename(src_1))
Gribouillis likes this post
Reply
#3
Hi, thank you for replying.

I want the print-out to be whatever folder I am in. So if I'm in folder "December_2021" it should print "December_2021". If I'm in "May_2022" it should print that.

Always the one I'm in. That's supposed to be the variable. And in my file structure it is the last folder. That's how it is on my computer.

The post is really about the variable. I can't make that work as you can see...
Reply
#4
If you want the name of the current working directory, use
from pathlib import Path
name = Path.cwd().name
If you want the name of the directory where the file executing this code is use
name = Path(__file__).parent.name
Reply
#5
(May-21-2022, 09:01 PM)User3000 Wrote: Please post all code, output and errors (it it's entirety) between their respective tags.
i don't know if you have this saved as an action button since it is needed so often. it needs s/it it/in it/
Gribouillis likes this post
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 466 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,391 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  Move file from one folder to another folder with timestamp added end of file shantanu97 0 2,438 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  Python Cut/Copy paste file from folder to another folder rdDrp 4 4,945 Aug-19-2020, 12:40 PM
Last Post: rdDrp
  Delete directories in folder is not working after folder is updated asheru93 2 2,601 Feb-13-2019, 12:37 PM
Last Post: asheru93
  copy content of folder to existing folder shlomi27 0 2,614 Aug-11-2018, 01:44 PM
Last Post: shlomi27

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020