Question on File Input and AST

J

joy99

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.

If any one can kindly give one or two small examples. It would help me
much.

Best Regards,
Subhabrata.
 
S

Steven D'Aprano

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:

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

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

joy99

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:

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

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

Dear Sir,

Thank you for your kind reply. I will have a check on them soon.

Warm Regards,
Subhabrata.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top