File handling - reading filenames

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 7
Reputation: bol0gna is an unknown quantity at this point 
Solved Threads: 0
bol0gna bol0gna is offline Offline
Newbie Poster

File handling - reading filenames

 
0
  #1
Oct 24th, 2009
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.

  1.  
  2. import os
  3.  
  4. maleDataSets = 0
  5. femaleDataSets = 0
  6. females = []
  7. males = []
  8.  
  9. filePath = "Dataset/parameter feature vectors"
  10. for file in os.listdir(filePath):
  11. femaleDataSets = file.find("female")
  12. if femaleDataSets == -1:
  13. females.append(femaleDataSets)
  14. print file
  15. else:
  16. 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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,062
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 936
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #2
Oct 24th, 2009
Try this ...
  1. import os
  2.  
  3. maleDataSets = 0
  4. femaleDataSets = 0
  5. females = []
  6. males = []
  7.  
  8. filePath = "Dataset/parameter feature vectors"
  9. for fname in os.listdir(filePath):
  10. data_str = open(fname).read()
  11. # returns index of first "female" find
  12. # returns -1 if not found
  13. index = data_str.find("female")
  14. if index != -1:
  15. females.append(index)
  16. print fname
  17. else:
  18. males.append(...)
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: bol0gna is an unknown quantity at this point 
Solved Threads: 0
bol0gna bol0gna is offline Offline
Newbie Poster
 
0
  #3
Oct 24th, 2009
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'


  1. filePath = "Dataset/parameter feature vectors"
  2. for fname in os.listdir(filePath):
  3. data_str = open(fname).read()
  4. index = data_str.find("female")
  5. if index != -1:
  6. females.append(index)
  7. print fname
  8. else:
  9. # append the ones that aren't female to a males
Last edited by bol0gna; Oct 24th, 2009 at 3:12 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,035
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 290
woooee woooee is offline Offline
Veteran Poster
 
0
  #4
Oct 24th, 2009
Always use absolute path+file names.
  1. filePath = "Dataset/parameter feature vectors"
  2. for fname in os.listdir(filePath):
  3. complete_name = os.path.join(filePath, fname)
  4. data_str = open(complete_name).read()
  5. index = data_str.find("female")
  6. if index != -1:
  7. females.append(index)
  8. print fname
  9. else:
  10. print "append the ones that aren't female to a males"
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: bol0gna is an unknown quantity at this point 
Solved Threads: 0
bol0gna bol0gna is offline Offline
Newbie Poster
 
0
  #5
Oct 24th, 2009
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?

  1. filePath = "Dataset/parameter feature vectors"
  2. for fname in os.listdir(filePath):
  3. complete_name = os.path.join(filePath, fname)
  4. data_string = open(complete_name).read()
  5. index = data_string.find("female")
  6. if index != -1:
  7. females.append(index)
  8. print fname
  9. else:
  10. 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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: bol0gna is an unknown quantity at this point 
Solved Threads: 0
bol0gna bol0gna is offline Offline
Newbie Poster
 
0
  #6
Oct 24th, 2009
Thank you for the help everyone! These solutions all work for me, I was indenting incorrectly. Apologies!
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 25
Reputation: The_Kernel is an unknown quantity at this point 
Solved Threads: 8
The_Kernel The_Kernel is offline Offline
Light Poster
 
1
  #7
Oct 25th, 2009
Originally Posted by woooee View Post
Always use absolute path+file names.
  1. filePath = "Dataset/parameter feature vectors"
  2. for fname in os.listdir(filePath):
  3. complete_name = os.path.join(filePath, fname)
  4. data_str = open(complete_name).read()
  5. index = data_str.find("female")
  6. if index != -1:
  7. females.append(index)
  8. print fname
  9. else:
  10. 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:
  1. filePath = "Dataset/parameter feature vectors"
  2. for fname in os.listdir(filePath):
  3. if fname.count('female'):
  4. females.append(fname)
  5. elif fname.count('male'):
  6. males.append(fname)
Last edited by The_Kernel; Oct 25th, 2009 at 4:02 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,062
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 936
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #8
Oct 25th, 2009
Originally Posted by The_Kernel View Post
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:
  1. filePath = "Dataset/parameter feature vectors"
  2. for fname in os.listdir(filePath):
  3. if fname.count('female'):
  4. females.append(fname)
  5. elif fname.count('male'):
  6. 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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC