My file directory is full of filenames with "male" and "female" being part of them. I want to loop through the directory and distinguish between the two.

import os

maleDataSets = 0
femaleDataSets = 0
females = []
males = []

filePath = "Dataset/parameter feature vectors"
for file in os.listdir(filePath):
    femaleDataSets = file.find("female")
    if femaleDataSets == -1:
        females.append(femaleDataSets)
        print file
    else:
        males.append(...)

I'd like to be able to say... if femaleDataSets are found, print their filenames - just to show that I am reading the correct files. Not sure if I am searching correctly. Any help would be greatly appreciated!

Try this ...

import os

maleDataSets = 0
femaleDataSets = 0
females = []
males = []

filePath = "Dataset/parameter feature vectors"
for fname in os.listdir(filePath):
    data_str = open(fname).read()
    # returns index of first "female" find
    # returns -1 if not found 
    index = data_str.find("female")
    if index != -1:
        females.append(index)
        print fname
    else:
        males.append(...)

Thanks for your reply. The first file in my directory isn't female, I don't think that has anything to do with this... but I am getting this error:

data_str = open(fname).read()
IOError: [Errno 2] No such file or directory: 'Bmale_22.b_app'

filePath = "Dataset/parameter feature vectors"
for fname in os.listdir(filePath):
    data_str = open(fname).read()
    index = data_str.find("female")
    if index != -1:
        females.append(index)
        print fname
    else:
        # append the ones that aren't female to a males

Always use absolute path+file names.

filePath = "Dataset/parameter feature vectors"
for fname in os.listdir(filePath):
    complete_name = os.path.join(filePath, fname)
    data_str = open(complete_name).read()
    index = data_str.find("female")
    if index != -1:
        females.append(index)
        print fname
    else:
        print "append the ones that aren't female to a males"

Not quite sure why this is happening... the only output I get here is "male" looped over many times...could it be the way we are searching?

filePath = "Dataset/parameter feature vectors"
for fname in os.listdir(filePath):
    complete_name = os.path.join(filePath, fname)
    data_string = open(complete_name).read()
    index = data_string.find("female")
    if index != -1:
        females.append(index)
        print fname
    else:
        print "male"

example of a few filenames:
BMale_22.b_app
EMBfemale20-2neutral.b_app
EMIfemale26neutral.b_app
EMImale23-2neutral.b_app

Would a regular expression be better?

Thank you for the help everyone! These solutions all work for me, I was indenting incorrectly. Apologies!

Always use absolute path+file names.

filePath = "Dataset/parameter feature vectors"
for fname in os.listdir(filePath):
    complete_name = os.path.join(filePath, fname)
    data_str = open(complete_name).read()
    index = data_str.find("female")
    if index != -1:
        females.append(index)
        print fname
    else:
        print "append the ones that aren't female to a males"

I don't think this is doing what bol0gna wants actually. In the original post bol0gna wants to sort on the filenames, while your code is actually searching the content of each file. Here's a version that works on the filename:

filePath = "Dataset/parameter feature vectors"
for fname in os.listdir(filePath):
    if fname.count('female'):
        females.append(fname)
    elif fname.count('male'):
        males.append(fname)

I don't think this is doing what bol0gna wants actually. In the original post bol0gna wants to sort on the filenames, while your code is actually searching the content of each file. Here's a version that works on the filename:

filePath = "Dataset/parameter feature vectors"
for fname in os.listdir(filePath):
    if fname.count('female'):
        females.append(fname)
    elif fname.count('male'):
        males.append(fname)

Looking at all the clues sprinkled in, you are so right. He only wants to look at filenames, so kudos to you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.