Python Forum
scandir() recursively and return path + filename
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
scandir() recursively and return path + filename
#2
Hello malonn, in that code you have some issues to work with, you can append de base directory to the entry name using the os.joinpath function

for Python 2 you have the scandir function in another module, like this

import os
import scandir
def scanRecurse(baseDir):
    for entry in scandir.scandir(baseDir):
        if entry.is_file():
            yield os.path.join(baseDir, entry.name)
        else:
            yield scanRecurse(entry.path)
 
for i in scanRecurse('D:\\App Back-Ups'):
    print(i)
but in python 3, scandir belongs to the os module

import os
def scanRecurse(baseDir):
    for entry in os.scandir(baseDir):
        if entry.is_file():
            yield os.path.join(baseDir, entry.name)
        else:
            yield scanRecurse(entry.path)
 
for i in scanRecurse('D:\\App Back-Ups'):
    print(i)
Remember to fix a bug in one of the yield statements

- The Python Tutor
Reply


Messages In This Thread
RE: scandir() recursively and return path + filename - by thepythotutor - May-09-2018, 02:46 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Modifying a dictionary recursively SpongeB0B 2 97 Yesterday, 04:09 PM
Last Post: Gribouillis
  How to return the next page from json recursively? sandson 0 1,197 Apr-01-2022, 11:01 PM
Last Post: sandson
  Help Needed | Read Outlook email Recursively & download attachment Vinci141 1 4,137 Jan-07-2022, 07:38 PM
Last Post: cubangt
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,251 Sep-09-2021, 01:25 PM
Last Post: Yoriz
Star Recursively convert nested dicts to dict subclass Alfalfa 1 2,935 Jan-22-2021, 05:43 AM
Last Post: buran
  Get the filename from a path 12237ee1 7 3,600 Jul-13-2020, 06:01 PM
Last Post: 12237ee1
  Python script that recursively zips folders WITHOUT nesting the folder inside the zip umkc1 1 2,904 Feb-11-2020, 09:12 PM
Last Post: michael1789
  How to get full path of specified hidden files matching pattern recursively SriRajesh 4 4,009 Jan-18-2020, 07:12 PM
Last Post: SriRajesh
  Find a given file recursively inside a directory alinaveed786 1 1,976 Jul-01-2019, 01:53 PM
Last Post: ichabod801
  Return all Path value from function Palerm0_24 2 2,580 Mar-18-2019, 03:09 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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