faqts : Computers : Programming : Languages : Python : Snippets : Stream Manipulation : Files

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 5 people (100%) answered Yes
Recently 4 of 4 people (100%) answered Yes

Entry

Applying a filter to all files in a directory

Jul 5th, 2000 09:59
Nathan Wallace, unknown unknown, Hans Nowak, Snippet 65, Fredrik Lundh


"""
Packages: files
"""

"""
>     i have a question to which i'm sure there is a simple answer, but i
> couldn't find any references.  i've written a program that converts a
> file from one format to another.  now, i'd like to write a python script
> that, when given a directory name, will apply this converter program to
> all the files in the specified directory.  what is the best way to do
> this?  i'm using a UNIX machine, but am unsure how to use the Python
> file manipulation libraries.
"""

import os, sys
for file in filter(os.path.isfile, os.listdir(sys.argv[1])):
    process(file)

"""
to process text files, you can pass the file list to
fileinput.input instead, and use the inplace option.
see the fileinput docs for details.

</F>
"""