Python Forum
print notifcation when enter new directory os.walk()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print notifcation when enter new directory os.walk()
#3
Making use of pathlib
import os
from pathlib import Path

base_path = Path(r'C:\Users\adam\Desktop')
current_folder = ''

for dirName, subdirList, fileList in os.walk(base_path):
    new_path = Path(dirName)
    relative_path = new_path.relative_to(base_path)
    if relative_path.parts and relative_path.parts[0] != current_folder:
        current_folder = relative_path.parts[0]
        print(f'enter new dir : {current_folder}')

    print(dirName)
Reply


Messages In This Thread
RE: print notifcation when enter new directory os.walk() - by Yoriz - Jun-20-2019, 08:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Smile Python & MSGraph - Learning to Walk ITMan020324 2 469 Feb-04-2024, 04:37 PM
Last Post: ITMan020324
  EasySNMP Walk/BulkWalk pylance 3 2,153 Nov-29-2021, 12:00 PM
Last Post: pylance
  How to sort os.walk list? Denial 6 11,792 Oct-10-2020, 05:28 AM
Last Post: Denial
  os.walk question DPaul 2 2,377 May-31-2020, 02:08 PM
Last Post: DPaul
  os.walk(Path("path_string")) giving error Kumarkv 4 3,934 May-10-2020, 08:46 AM
Last Post: snippsat
  os.walk does not see files that are in the folder kiton 1 3,085 Jan-21-2020, 07:26 PM
Last Post: micseydel
  Animating Random Walk Excelsiscelia 2 5,284 Nov-24-2018, 08:53 AM
Last Post: Excelsiscelia
  Python3 & os.walk while_e_coyote 2 3,508 Aug-24-2018, 07:32 PM
Last Post: while_e_coyote
  how to test if an object came from os.walk() Skaperen 3 3,230 Jan-10-2018, 03:05 AM
Last Post: Skaperen
  Question about os.walk() behaviour dfkettle 3 4,441 Jan-07-2018, 06:52 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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