0
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
3
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by pyTony
0

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)
        print(full_name)
        # 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 by vegaseat: n/a

0

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 by pyTony: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.