Computing.Net > Forums > Programming > operate several files in python?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

operate several files in python?

Reply to Message Icon

Name: zhynxn
Date: July 6, 2006 at 20:00:31 Pacific
OS: FC5
CPU/Ram: 1G
Product: Fedora
Comment:

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 ./dst

I want to know how to realize it in python. I am a beginner of Python.
Thank you for your help.



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: July 7, 2006 at 04:05:21 Pacific
Reply:

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.html

If 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.


0

Response Number 2
Name: zhynxn
Date: July 7, 2006 at 09:39:58 Pacific
Reply:

thanks a lot.
I have solved my problem.
Python is very interesting and conveinent.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Hexadecimal to WORD conve... Java terms



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: operate several files in python?

Determine length of file in Python www.computing.net/answers/programming/determine-length-of-file-in-python/11903.html

opening an image in python www.computing.net/answers/programming/opening-an-image-in-python/14922.html

Working With Files in Binary in C++ www.computing.net/answers/programming/working-with-files-in-binary-in-c/4520.html