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
#1
I have this code:
1
2
3
4
5
6
7
8
9
10
11
import os
 
def scanRecurse(baseDir):
    for entry in os.scandir(baseDir):
        if entry.is_file():
            yield entry.name
        else:
            yield from scanRecurse(entry.path)
 
for i in scanRecurse('D:\\App Back-Ups'):
    print(i)
it returns all files within the directory and subdirectories. How can I modify it to return the files with their full paths?
Reply
#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

1
2
3
4
5
6
7
8
9
10
11
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

1
2
3
4
5
6
7
8
9
10
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
#3
(May-09-2018, 01:12 AM)malonn Wrote: it returns all files within the directory and subdirectories. How can I modify it to return the files with their full paths?
It's easier to use os.walk(),which is recursive bye default.
Then just join root and files for full paths.
1
2
3
4
5
import os
 
for root,dirs,files in os.walk('D:\\App Back-Ups'):
    for file in files:
        print(os.path.join(root, file))
Reply
#4
Thanks @thepythotutor and @snippsat. I hadn't even looked at "walk()" because the docs say how much more efficient scandir() is. I think I'll stick with good ol' scandir() for now.
Also, @thepythotutor, the line...

1
yield from scanRecurse(entry.path)
...is correct. I believe you said it was bugged (if that is what you meant)?
Reply
#5
(May-09-2018, 01:31 PM)malonn Wrote: @snippsat. I hadn't even looked at "walk()" because the docs say how much more efficient scandir() is.
os.walk() also use scandir PEP 471.
Quote:As part of this proposal, os.walk() will also be modified to use scandir() rather than listdir() and os.path.isdir().
This will increase the speed of os.walk() very significantly (as mentioned above, by 2-20 times, depending on the system).
Reply
#6
Ah. Cool. I did not read that particular tidbit. Knowing that, I will switch; walk() uses fewer lines of code. Thanks for clearing that up for me, @snippsat.
Reply
#7
os.walk is powerful. You will enjoy it
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


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