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...
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 :-) - def dir_list5(dir_name, *args):
-
fileList = []
-
for file in os.listdir(dir_name):
-
dirfile = os.path.join(dir_name, file)
-
if os.path.isfile(dirfile): # skip directories
-
if len(args) == 0:
-
fileList.append('%s\n' %(dirfile))
-
else:
-
if os.path.splitext(dirfile)[1][1:] in args:
-
fileList.append('%s\n' %(dirfile))
-
return "".join(fileList)
-
-
if __name__ == '__main__':
-
-
def run_script():
-
-
dir_name = (os.path.join('H:\\', 'TEMP', 'temsys'))
-
fList = dir_list2(dir_name, 'txt')
-
data_file = (os.path.join('H:\\', 'TEMP', 'temsys', 'student_data.dat'))
-
-
try:
-
fData = open(data_file, 'w')
-
-
for f in fList.split():
-
-
ff = open(f, "r")
-
print f
-
for line in ff:
-
if 'student ID:' in line:
-
# print line[line.index('student ID:')+11:]
-
print line.split('student ID:')[1]
-
fData.write("".join([os.path.basename(f),'----',line.split('student ID:')[1]]))
-
ff.close()
-
-
fData.close()
-
-
except:
-
raise IOError, "Could not open data file or file open error"
-
-
import profile
-
#profile.run('run_script()')
-
run_script()
Output: - >>> H:\TEMP\temsys\abc1.txt
-
stu644850
-
-
H:\TEMP\temsys\abc2.txt
-
stu7739895
-
-
H:\TEMP\temsys\abc3.txt
-
stu77398
-
-
H:\TEMP\temsys\abc4.txt
-
stu3885
-
>>>
Data file contents: - abc1.txt----stu644850
-
abc2.txt----stu7739895
-
abc3.txt----stu77398
-
abc4.txt----stu3885
Of course you need to substitute your specific information.
bvdet 2,851
Expert Mod 2GB
By adding a break statement in the for loop, the iteration stops when the student ID is found: - def run_script():
-
-
dir_name = (os.path.join('H:\\', 'TEMP', 'temsys'))
-
fList = dir_list2(dir_name, 'txt')
-
data_file = (os.path.join('H:\\', 'TEMP', 'temsys', 'student_data.dat'))
-
-
try:
-
fData = open(data_file, 'w')
-
print
-
for f in fList.split():
-
-
ff = open(f, "r")
-
print f
-
for line in ff:
-
if 'student ID:' in line:
-
print line.split('student ID:')[1]
-
fData.write("".join([os.path.basename(f),'----',line.split('student ID:')[1]]))
-
break # <------------- ADDED THE BREAK HERE
-
ff.close()
-
-
fData.close()
-
-
except:
-
raise IOError, "Could not open data file or file open error"
Thanks bv !!!!!!!!
It's really useful to me...
Thanks alot...
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. :-)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
| |