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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:18:56+00:00 2026-06-05T03:18:56+00:00

i have to process .txt files presnent in subfolder inside a Folder.like: New Folder>Folder

  • 0

i have to process .txt files presnent in subfolder inside a Folder.like:
New Folder>Folder 1 to 6>xx.txt & yy.txt(files present in each folder)
each file contain two columns as:

arg  his
asp  gln
glu  his

and

arg his
glu arg
arg his
glu asp

now what I have to do is :
1)count number of occurance of each word for each file > and average total count by dividing with total no. of lines in that file
2)then with values obtained after completing 1st step, divide the values with total no. of files present in the folder for averaging (i.e. 2 in this case)
I have tried with my code as follows:
but I have succeeded in 1st case but I’m not getting 2nd case.

for root,dirs,files in os.walk(path):
    aspCount = 0
    glu_count = 0
    lys_count = 0
    arg_count = 0
    his_count = 0
    acid_count = 0
    base_count = 0
    count = 0
    listOfFile = glob.iglob(os.path.join(root,'*.txt')
    for filename in listOfFile:
        lineCount = 0
        asp_count_col1 = 0
        asp_count_col2 = 0
        glu_count_col1 = 0
        glu_count_col2 = 0
        lys_count_col1 = 0
        lys_count_col2 = 0
        arg_count_col1 = 0
        arg_count_col2 = 0
        his_count_col1 = 0
        his_count_col2 = 0
        count += 1
        for line in map(str.split,inp):
            saltCount += 1
            k = line[4]
            m = line[6]
            if k == 'ASP':
               asp_count_col1 += 1
            elif m == 'ASP':
               asp_count_col2 += 1
            if k == 'GLU':
               glu_count_col += 1
            elif m == 'GLU':
                glu_count_col2 += 1
            if k == 'LYS':
                lys_count_col1 += 1
            elif m == 'LYS':
                lys_count_col2 += 1
            if k == 'ARG':
                arg_count_col1 += 1
            elif m == 'ARG':
                arg_count_col2 += 1
            if k == 'HIS':
                his_count_col1 += 1
            elif m == 'HIS':
                his_count_col2 += 1
        asp_count = (float(asp_count_col1 + asp_count_col2))/lineCount   
        glu_count = (float(glu_count_col1 + glu_count_col2))/lineCount   
        lys_count = (float(lys_count_col1 + lys_count_col2))/lineCount   
        arg_count = (float(arg_count_col1 + arg_count_col2))/lineCount   
        his_count = (float(his_count_col1 + his_count_col2))/lineCount   

upto this I could be able to get the average value per file. But how could I be able to get average per subfolder(i.e. by dividing with count(total no. of file)).
the problem is 2nd part. 1st part is done. The code provided will average values for each file. But I want to add this averages and make a new average by dividing with total no. of files present in the sub-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-06-05T03:18:58+00:00Added an answer on June 5, 2026 at 3:18 am

    Your use of os.walk together with glob.iglob is bogus. Either use one or the other, not both together. Here’s how I would do it:

    import os, os.path, re, pprint, sys
    #...
    for root, dirs, files in os.walk(path):
      counts = {}
      nlines = 0
      for f in filter(lambda n: re.search(r'\.txt$', n), files):
        for l in open(f, 'rt'):
          nlines += 1
          for k in l.split():
            counts[k] = counts[k]+1 if k in counts else 1
      for k, v in counts.items():
        counts[k] = float(v)/nlines
    
      sys.stdout.write('Frequencies for directory %s:\n'%root
      pprint.pprint(counts)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Do files opened like file(foo.txt) have any info about file modification time? Basically I
I have a file FileList.txt with this text: /home/myusername/file1.txt ~/file2.txt ${HOME}/file3.txt All 3 files
Possible Duplicate: how to read all files inside particular folder I have the following
I need to glob ./../path/to/files/**/*.txt but instead of receiving matches like this: ./../path/to/files/subdir/file.txt I
I have set of flat files (114 files) each file is named with database
I have a process intensive task that I would like to run in the
I have to process XML files that have a DTD with a XSLT in
Here is my dillema... I have a directory full of .txt comma delimited files
I have two files listed in input.txt, I can read in the first one
I have a program that processes files and returns another file as output. When

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.