0

I'm new to python and want to code for specific characteristics in folders and then perform operations on the contents within the desired folder. This is an example below:

Path = "./Desktop/Example/"          #Input Directory
A_files = []                                                                    
for dirName, subdirList, fileList in os.walk(Path):                            
    for filename in fileList:                                                       
        if "[A]" in subdirList()
        if ".txt" in filename.lower():                                                  
            A_files.append(os.path.join(dirName,filename)) 

#Perform Mathematical Operators for A_files only

B_files = []                                                                    
for dirName, subdirList, fileList in os.walk(Path):                            
    for filename in fileList:                                                       
        if "[B]" in subdirList()
        if ".txt" in filename.lower():                                                  
            B_files.append(os.path.join(dirName,filename))

 #Perform Mathematical Operators for B_files only

 #Perform Mathematical Operators between A_files and B_files together

In folder "Example" are sub folders: Hello_World[A] and Hello_World[B] and I want to access all .txt files in each folder individually at first to perform a scaling of the values. Later I do other mathematical operators between the two files.

Thank you for you help!

Tyler_Cork
  • 21
  • 3
  • 3
    What's your question? – Kevin Nov 10 '15 at 17:19
  • How do I select a folder based on only a part of the name? Like in the last if statement I am able to select only .txt files I only want to choose the folder that contain [A] in it – Tyler_Cork Nov 10 '15 at 17:23
  • I think the `glob` module can do wildcard-style partial matching. I haven't used it myself. – Kevin Nov 10 '15 at 17:24
  • The thing is I know over time the prefix is going to change but i know the [A] convention and the [B] convention will remain the same. I need to find a way to isolate Hello_World[A] and Hi_World[B] through the directories – Tyler_Cork Nov 10 '15 at 17:35

1 Answers1

0

I think you are asking for something like this:

Path = "./Desktop/Example/"          #Input Directory
A_files = []                                                                    
for dirName, subdirList, fileList in os.walk(Path):                            
    for filename in fileList:                                                       
        if "[A]" in dirName:
            if ".txt" in filename.lower():                                                  
                A_files.append(os.path.join(dirName,filename)) 

#Perform Mathematical Operators for A_files only

B_files = []                                                                    
for dirName, subdirList, fileList in os.walk(Path):                            
    for filename in fileList:                                                       
        if "[B]" in dirName:
            if ".txt" in filename.lower():                                                  
                B_files.append(os.path.join(dirName,filename))

 #Perform Mathematical Operators for B_files only
 for f in B:
     data = open(filename).read()
     # Do stuff

 #Perform Mathematical Operators between A_files and B_files together
 for index, file in enumerate(A):
     dataA = open(A[i]).read()
     dataB = open(B[i]).read()
     # Do something

Since dirname is a string, you can use in to check its contents.

Then, you can iterate over your A[] and B[] to do your math, and use enumerate to iterate over both using an index. See here: Accessing the index in Python 'for' loops

Community
  • 1
  • 1
Will
  • 3,611
  • 5
  • 26
  • 47