473,385 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
4 1808
bvdet
2,851 Expert Mod 2GB
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
2,851 Expert Mod 2GB
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
Thanks bv !!!!!!!!

It's really useful to me...

Thanks alot...
Jan 8 '07 #4
bvdet
2,851 Expert Mod 2GB
Thanks bv !!!!!!!!

It's really useful to me...

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

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

Similar topics

2
by: Stephen Horne | last post by:
Just recently I decided I want to make use of my ISP freebie webspace. In order to make that easier, I'd like to be able to automatically synchronise an FTP file/folder heirarchy with one on my...
6
by: Bruce | last post by:
I am doing development work on an asp.net app and a web service on a Win XP Pro SP2 development workstation, using vs.net 2003. For reasons that I wont try to explain here, I recently moved the...
0
by: troutbum | last post by:
I am experiencing problems when one user has a document open through a share pointing to the web site. I use the dsolefile to read the contents of a particular directory and then display them in a...
3
by: kevin.koehne | last post by:
I'm having a problem building a solution in VS2003 (.NET 1.1) on Windows XP. I get the error: "Unable to open Web project 'projname'. The file path 'C:\app-1.6.0.600\solution\projname' does not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.