Question on File Input and AST
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sun Sep 6 04:49:46 EDT 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
> BOTH
> [Either of 1 or 2 to open as the need be] 3. read_file=open_file.read()
> 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:
[quote]
Typical use is:
import fileinput
for line in fileinput.input():
process(line)
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:
http://docs.python.org/dev/whatsnew/2.6.html#the-ast-module
and a discussion here:
http://stackoverflow.com/questions/768634/python-parse-a-py-file-read-the-
ast-modify-it-then-write-back-the-modified
If you explain what you want to do, perhaps there's somebody out there
who knows the ast module and can answer.
--
Steven
More information about the Python-list
mailing list