Question on File Input and AST

Steven D'Aprano steve at
Sun Sep 6 10:49:46 CEST 2009

On Sun, 06 Sep 2009 00:53:43 -0700, joy99 wrote:

> Dear Group,
> I have a file "test1.txt". Now, as I do the file handling, i try to do
> any one of the following operations.
> 1. open_file=open("/python26/test1.txt","r") # FOR READING 2.
> open_file=open("/python26/test1.txt","r+") # FOR READING AND WRITING
> [Either of 1 or 2 to open as the need be] 3.
> etc...
> But how to work with "fileinput" or "ast".
> I tried to read python docs and some backlog question answer in the
> group but it did not help much.

At the interactive interpreter, do this:

>>> import fileinput
>>> help(fileinput)

and read the help text. It gives you an example:

    Typical use is:

        import fileinput
        for line in fileinput.input():

    This iterates over the lines of all files listed in sys.argv[1:],
    defaulting to sys.stdin if the list is empty.  If a filename is '-' it
    is also replaced by sys.stdin.  To specify an alternative list of
    filenames, pass it as the argument to input().  A single file name is
    also allowed.
    [end quote]

If there is something unclear about that, then please try to be specific 
about what you don't understand. Try some code, and report what errors 
you get.

As far as ast, there's an example here:

and a discussion here:

If you explain what you want to do, perhaps there's somebody out there 
who knows the ast module and can answer.


More information about the Python-list mailing list