hi , once gain i need a suggestion from the expert, the prob is that i have 400 folder(folderxyz) in a given directory, and each folder contains some file . now i want to open directory , open each folder one by one and read the files present in it
import os
import sys
import re
import glob
folder_list = os.listdir(os.getcwd())
path = '/global/group/home/ktrip_01/work/whole_proteome/analysis_xenopus_takifugu_tetradon/split_result/filtered_domain/ORTHOMCLV1.4
f for f in folder_list:
         if os.path.isdir()os.path.join(dirname,f):
                       print f
                       files = os.listdir(f)
                       for f in files:
                              if f startswith(all_orthomcl):
                                       fr = open(f,'r')
                                       frlines= fr.readlines()
                                       for line in frlines:
                                             print line

Here is one way to do this ...

# list filenames with full path in a folder and its subfolders

import os

# use current folder or give a folder name
folder = os.getcwd()

for (paths, dirs, files) in os.walk(folder):
    for fname in files:
        full_name = os.path.join(paths, fname)
        # now you can set the conditions to look at each file
        # or append to a full_name list to process later

Note: don't use tabs for indentations

Edited 5 Years Ago by vegaseat: n/a

Small nit pick, vegaseat. Because your use of os.path.join at line 10, looks like paths variable is scalar and should be better named in singular, path, to not to be misleading.

Edited 5 Years Ago by pyTony: n/a

This article has been dead for over six months. Start a new discussion instead.