Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
yesterday, I asked how to delte records in several files with index record in another file. Thanks ghostdog's great help.
I still wanna know how to run the same operation on several files, and same the results with similiar names in another directory. For example:The source files are file1.dat, file2.dat, file3.dat in ./src
I want to delete the $1 record on them.
Then save the resulte files as file1.fin, file2.fin, file3.fin in ./dstI want to know how to realize it in python. I am a beginner of Python.
Thank you for your help.

hi
glad you are interested in python..it has helped me a lot for most of the task i do everyday..it is simple and easy to use..i am sure it can help you too.(even perl can be used...)if you are using newer version of python , prob 2.3 or above, there is a module called fileinput.
here's a link to get more information
http://docs.python.org/lib/module-fileinput.htmlIf you want too , can also use a for loop to iterate the files...
files = ['file1','file2','file3']
for fi in files:
do_something(fi)the do_something() function will be the function that read in the files and process the lines.
if you do not know the file names beforehand, you can use the os module's listdir() function to get the files.
eg..
import os
thedir = os.path.join("/somedir","src")
os.chdir(thedir)
for files in os.listdir(thedir):
if os.path.isfile(files): #if it's a file
do_something(files)def do_something(file):
'''function to perform the deletion of
records in the files and stuffs....
you can put in the code i posted here..
'''
For moreinformation, read up the python docs at i have given you above..
hope it helps.
BTW, as this site does not do space formatting (or does it? )its difficult to see what it is like, as python uses indentation, rather than braces {} for code blocks...so take note.

![]() |
Hexadecimal to WORD conve...
|
Java terms
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |