By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,450 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

how to open a directory and i should read the files and need a particular line

P: 24
HI,

Need your help please...
First thing, i want to open a directory and with in the directory i have 1000's of files and we need to read the file and i want a particular line which has the word "ID".

for example:

in a directory... i have file like the below.
-) abc1
-) abc2
-) abc3 like this i have 1000 files...

in the file abc1 -> i have a line called =>:student ID:stu12312
like that, each and every file has student ID...

The O/P should be like....

abc1 ------ stu12312
abc2 ------ stu........

.
.
.
.

Like this I need a output...

Please it's urgent...

Thanks in advance...
Jan 5 '07 #1
Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
HI,

Need your help please...
First thing, i want to open a directory and with in the directory i have 1000's of files and we need to read the file and i want a particular line which has the word "ID".

for example:

in a directory... i have file like the below.
-) abc1
-) abc2
-) abc3 like this i have 1000 files...

in the file abc1 -> i have a line called =>:student ID:stu12312
like that, each and every file has student ID...

The O/P should be like....

abc1 ------ stu12312
abc2 ------ stu........

.
.
.
.

Like this I need a output...

Please it's urgent...

Thanks in advance...
I am getting a lot of exercise in file I/O :-)
Expand|Select|Wrap|Line Numbers
  1. def dir_list5(dir_name, *args):
  2.     fileList = []
  3.     for file in os.listdir(dir_name):
  4.         dirfile = os.path.join(dir_name, file)
  5.         if os.path.isfile(dirfile): # skip directories
  6.             if len(args) == 0:
  7.                 fileList.append('%s\n' %(dirfile))
  8.             else:
  9.                 if os.path.splitext(dirfile)[1][1:] in args:
  10.                     fileList.append('%s\n' %(dirfile))
  11.     return "".join(fileList)
  12.  
  13. if __name__ == '__main__':
  14.  
  15.     def run_script():
  16.  
  17.         dir_name = (os.path.join('H:\\', 'TEMP', 'temsys'))
  18.         fList = dir_list2(dir_name, 'txt')
  19.         data_file = (os.path.join('H:\\', 'TEMP', 'temsys', 'student_data.dat'))
  20.  
  21.         try:
  22.             fData = open(data_file, 'w')
  23.  
  24.             for f in fList.split():
  25.  
  26.                 ff = open(f, "r")
  27.                 print f
  28.                 for line in ff:
  29.                     if 'student ID:' in line:
  30.                         # print line[line.index('student ID:')+11:]
  31.                         print line.split('student ID:')[1]
  32.                         fData.write("".join([os.path.basename(f),'----',line.split('student ID:')[1]]))
  33.                 ff.close()
  34.  
  35.             fData.close()
  36.  
  37.         except:
  38.             raise IOError, "Could not open data file or file open error"
  39.  
  40.     import profile
  41.     #profile.run('run_script()')
  42.     run_script()
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> H:\TEMP\temsys\abc1.txt
  2. stu644850
  3.  
  4. H:\TEMP\temsys\abc2.txt
  5. stu7739895
  6.  
  7. H:\TEMP\temsys\abc3.txt
  8. stu77398
  9.  
  10. H:\TEMP\temsys\abc4.txt
  11. stu3885
  12. >>> 
Data file contents:
Expand|Select|Wrap|Line Numbers
  1. abc1.txt----stu644850
  2. abc2.txt----stu7739895
  3. abc3.txt----stu77398
  4. abc4.txt----stu3885
Of course you need to substitute your specific information.
Jan 5 '07 #2

bvdet
Expert Mod 2.5K+
P: 2,851
By adding a break statement in the for loop, the iteration stops when the student ID is found:
Expand|Select|Wrap|Line Numbers
  1. def run_script():
  2.  
  3.         dir_name = (os.path.join('H:\\', 'TEMP', 'temsys'))
  4.         fList = dir_list2(dir_name, 'txt')
  5.         data_file = (os.path.join('H:\\', 'TEMP', 'temsys', 'student_data.dat'))
  6.  
  7.         try:
  8.             fData = open(data_file, 'w')
  9.             print
  10.             for f in fList.split():
  11.  
  12.                 ff = open(f, "r")
  13.                 print f
  14.                 for line in ff:
  15.                     if 'student ID:' in line:
  16.                         print line.split('student ID:')[1]
  17.                         fData.write("".join([os.path.basename(f),'----',line.split('student ID:')[1]]))
  18.                         break       # <------------- ADDED THE BREAK HERE
  19.                 ff.close()
  20.  
  21.             fData.close()
  22.  
  23.         except:
  24.             raise IOError, "Could not open data file or file open error"
Jan 5 '07 #3

P: 24
Thanks bv !!!!!!!!

It's really useful to me...

Thanks alot...
Jan 8 '07 #4

bvdet
Expert Mod 2.5K+
P: 2,851
Thanks bv !!!!!!!!

It's really useful to me...

Thanks alot...
You are welcome. I am glad we could help you. :-)
Jan 8 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.