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 7869513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:20:54+00:00 2026-06-03T01:20:54+00:00

Imagine several folders such as d:\myfolder\abc d:\myfolder\ard d:\myfolder\kjes … And in each folder, there

  • 0

Imagine several folders such as

d:\myfolder\abc
d:\myfolder\ard 
d:\myfolder\kjes
...

And in each folder, there are files such as

0023.txt, 0025.txt, 9932.txt in d:\myfolder\abc
2763.txt, 1872.txt, 0023.txt, 7623.txt in d:\myfolder\ard
2763.txt, 2873.txt, 0023.txt in d:\myfolder\kjes

So, there are three 0023.txt files, and two 2763.txt files.

I want to create a file (say, d:\myfolder\dup.txt) which contains the following information:

0023 3
0025 1
9932 1
2763 2
1872 1
7623 1
2873 1

How can I implement that in Python? Thanks.

  • 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-06-03T01:20:55+00:00Added an answer on June 3, 2026 at 1:20 am

    Not extensively tested, but this works:

    import os, os.path
    
    dupnames={}
    for root, dirs, files in os.walk('myfolder'):
        for file in files:
            fulpath=os.path.join(root,file)
            if file in dupnames:
                dupnames[file].append(fulpath)
            else:
                dupnames[file]=[fulpath]
    
    for name in sorted(dupnames):
        print name, len(dupnames[name])
    

    This works in the following way:

    1. Creates an empty dict;
    2. Walks the file hierarchy;
    3. Creates a entries in a dict of lists (or append an existing list) with the base name: [path to file].

    After the os.walk you will have a dict like so:

    {0023.txt: ['d:\myfolder\abc', 'd:\myfolder\kjes'], 0025.txt: ['d:\myfolder\abc']}
    

    So to get your output, just iterate over the sorted dict and count the entries in the list. You can either redirect the output of this to a file or open your output file directly in Python.

    You show your output with the extension stripped — 0023 vs 0023.txt. What should happen if you have 0023.txt and 0023.py? Same file or different? To the OS they are different files so I kept the extension. It is easily stripped if that is your desired output.

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

Sidebar

Related Questions

Imagine a JSF page with several components such as <h:selectOneMenu> , <h:dataTable> , <h:panelGrid>
I have an application where there several threads each pumping into a separate AudioTrack
Imagine you have a form where you switch visibility of several fields. And if
Imagine following scenario: We have a lot of parallel development going on in several
Imagine you have a website with several drop-downs that are populated from the back-end
Imagine an ASP.NET application with several theme defined within it. How can I change
Imagine a GPS tracking system that is following the position of several objects. The
Imagine if for any reason, you want to create several variables, and without using
Imagine you are adding several elements to an array: $array[] = 'dog'; $array[] =
Let's imagine we have several type of elements, and we want to create a

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.