Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6966055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:08:30+00:00 2026-05-27T16:08:30+00:00

I wish to avoid os.walk, i am using a recursive function to read files

  • 0

I wish to avoid os.walk, i am using a recursive function to read files and folders and store files to a dictionary

I got rid of the os.chdir but for some reason function is now joining path + file as well and its generating an error :
WindowsError: [Error 267] The directory name is invalid: ‘c:\data\foo\notes\*.*’
Its reading folder foo and it joined path whit foo and file notes.txt instad of foo + libary folder

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:08:31+00:00Added an answer on May 27, 2026 at 4:08 pm

    This seems to work for me

    import os
    
    op = os.path
    
    def fileRead(mydir):
        data = {}
        root = set()
        for i in os.listdir(mydir):
            path = op.join(mydir, i)
            print(path)
            if op.isfile(path):
                data.setdefault(i, set())
                root.add(op.relpath(mydir).replace("\\", "/"))
                data[i] = root
            else:
                data.update(fileRead(path))
        return data
    
    
    d = fileRead("c:\python32\programas")
    print(d)
    

    Still I am not sure why you use the set root. I think the purpose is to keep all the directories when you have the same file in two directories. But it doesnt work: each update deletes the stored values for repeated keys (file names).

    Here you have a working code, using a defaultdict /you can do the same with an ordinary dictionary (as in your code) but using defauldict you dont need to check if a key has been initialized before:

    import os
    from collections import defaultdict
    op = os.path
    
    def fileRead(mydir):
        data = defaultdict(list)
        for i in os.listdir(mydir):
            path = op.join(mydir, i)
            print(path)
            if op.isfile(path):
                root = op.relpath(mydir).replace("\\", "/")
                data[i].append(root)
            else:
                for k, v in fileRead(path).items():
                    data[k].extend(v)
        return data
    
    
    d = fileRead("c:\python32\programas")
    print(d)
    

    Edit: Relative to the comment from @hughdbrown:

    If you update data with data.update(fileRead(path).items()) you get tthis when calling for fileRead("c:/python26/programas/pack") in my computer (now in py26):

    c:/python26/programas/pack\copia.py
    c:/python26/programas/pack\in pack.py
    c:/python26/programas/pack\pack2
    c:/python26/programas/pack\pack2\copia.py
    c:/python26/programas/pack\pack2\in_pack2.py
    c:/python26/programas/pack\pack2\pack3
    c:/python26/programas/pack\pack2\pack3\copia.py
    c:/python26/programas/pack\pack2\pack3\in3.py

    defaultdict( ‘list’>, {‘in3.py’: [‘pack/pack2/pack3’], ‘copia.py’: [‘pack/pack2/pack3’],
    ‘in pack.py’: [‘pack’], ‘in_pack2.py’: [‘pack/pack2’]})

    Note that files that are repeated in several directories (copia.py) only show one of those directories, the deeper one. However all the directories are listed when using:

    for k, v in fileRead(path).items():  data[k].extend(v)
    

    c:/python26/programas/pack\copia.py
    c:/python26/programas/pack\in pack.py
    c:/python26/programas/pack\pack2
    c:/python26/programas/pack\pack2\copia.py
    c:/python26/programas/pack\pack2\in_pack2.py
    c:/python26/programas/pack\pack2\pack3
    c:/python26/programas/pack\pack2\pack3\copia.py
    c:/python26/programas/pack\pack2\pack3\in3.py

    defaultdict(, {‘in3.py’: [‘pack/pack2/pack3’], ‘copia.py’: [‘pack’, ‘pack/pack2’, ‘pack/pack2/pack3’],
    ‘in pack.py’: [‘pack’], ‘in_pack2.py’: [‘pack/pack2’]})

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to test a function that will generate lorem ipsum text, but it
I wish to avoid using css transitions. I have three divs of the same
I wish to know all the pros and cons about using these two methods.
I've got the No space left on device IOException when writing file. I wish
i need an advice on how organize my files and folders. First of all,
I wish to Avoid duplicated item being inserted. When I iterate through the collection
I wish to avoid switch statement by defining a pointer to a vector. I
I wish to know if there is any way to avoid to have a
I wish Subversion had a better way of moving tags. The only way that
I wish to implement a 2d bit map class in Python. The class would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.