| | |
File handling - reading filenames
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 7
Reputation:
Solved Threads: 0
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.
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!
Python Syntax (Toggle Plain Text)
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!
0
#2 Oct 24th, 2009
Try this ...
python Syntax (Toggle Plain Text)
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(...)
May 'the Google' be with you!
•
•
Join Date: Oct 2009
Posts: 7
Reputation:
Solved Threads: 0
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'
data_str = open(fname).read()
IOError: [Errno 2] No such file or directory: 'Bmale_22.b_app'
Python Syntax (Toggle Plain Text)
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
Last edited by bol0gna; Oct 24th, 2009 at 3:12 pm.
•
•
Join Date: Dec 2006
Posts: 1,035
Reputation:
Solved Threads: 290
0
#4 Oct 24th, 2009
Always use absolute path+file names.
Python Syntax (Toggle Plain Text)
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"
Linux counter #99383
•
•
Join Date: Oct 2009
Posts: 7
Reputation:
Solved Threads: 0
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?
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?
Python Syntax (Toggle Plain Text)
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?
•
•
Join Date: May 2009
Posts: 25
Reputation:
Solved Threads: 8
1
#7 Oct 25th, 2009
•
•
•
•
Always use absolute path+file names.Python Syntax (Toggle Plain Text)
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"
python Syntax (Toggle Plain Text)
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)
Last edited by The_Kernel; Oct 25th, 2009 at 4:02 am.
0
#8 Oct 25th, 2009
•
•
•
•
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:
python Syntax (Toggle Plain Text)
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)
May 'the Google' be with you!
![]() |
Similar Threads
- Trouble with file handling (Python)
- File Handling : Problem Reading end of Line (C++)
- File Handling. Implimentation problem. (C)
- please give me a simple file handling program (C)
- File Handling (C)
- C File handling - search within file without reading content? (C++)
Other Threads in the Python Forum
- Previous Thread: Two simple question
- Next Thread: Odd- Length Palindrome
| Thread Tools | Search this Thread |
accessdenied apache application argv array beginner book builtin change color converter countpasswordentry curved dan08 dictionary dynamic edit enter examples file filename float format function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl pysimplewizard python random recursion redirect remote reverse scrolledtext session simple smtp software sprite statictext string strings syntax table tennis terminal text textarea thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython






