Re: [Tutor] Seeking help with reading and writing files in Python

2012-07-08 Thread Alan Gauld

On 26/06/12 19:28, Aristotle wrote:


The sequence of events that I am using now is to open a program that
will gather gallons used from user, call a module to make calculations,
use the info from the module to write info to a file named
FinalProjectBill.txt.
...
 From Instructor:
You should read from one file (FinalProjectBill.txt) and then write to
one (BranleyFinal.txt) with the results as we are learning file i/o
(input/output) -
...
You should not write to your input file.



My question is why do I need two text files FinalProjectBill.txt and
BranleyJamesFinal.txt ?


I didn't read this properly the first time around.

The answer to your question is that you don't. It is a design decision.

But, if you write back to the input file it makes things more complex 
because you may have to read/write from the same file at the same 
time(*). This means moving the file cursor back and forth between the 
point where you are reading and the end of the file. There are functions 
to do this but it adds a lot of extra overhead and is easy to get wrong.
It is much simpler to read the data from one file and write the output 
to another and let Python take care of moving the cursor automatically.


(*)Of course you may choose to read all the input data in, close the 
file then overwrite it with your output. That's easy but sometimes the 
input file is too big or the processing time too limited to do that. Or 
you may need to preserve the data for use in subsequent processing.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Seeking help with reading and writing files in Python

2012-07-08 Thread David Rock
* Aristotle  [2012-06-26 14:28]:
> 
> The sequence of events that I am using now is to open a program that 
> will gather gallons used from user, call a module to make calculations, 
> use the info from the module to write info to a file named 
> FinalProjectBill.txt.
> 
> After that is done, I open another program that will read the info 
> written to FinalProjectBill.txt
> 
> I guess my confusion about the instructions is, if I should read from 
> one file FinalProjectBill.txt first how does data get written to that 
> file to begin with?

Based on this part, you have already answered your own question.
Specifically, you get your input file, FinalProjectBill.txt, by
collecting data from the user, doing some calculations, and outputting
to the file.

I'm not sure I see the problem. :-(

-- 
David Rock
da...@graniteweb.com


pgpgnpfBiWpdl.pgp
Description: PGP signature
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Seeking help with reading and writing files in Python

2012-07-04 Thread Alan Gauld

On 26/06/12 19:28, Aristotle wrote:


with an assignment that asks me to read from one file and then write to
another file. The problem is that the file to read from has not been
created yet.


So create it.
Any old text editor will do.
The whole point is, I believe that you are writing a program to read a 
file created by some other application, perhaps a web application or 
database reporting tool. (or in this case Notepad, say).


How the file is created is irrelevant so long as it has the data that 
you want to extract within it.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Seeking help with reading and writing files in Python

2012-07-04 Thread Aristotle

Hello,

I am reading Tony Gaddis "Teach Python" book and I am having trouble 
with an assignment that asks me to read from one file and then write to 
another file. The problem is that the file to read from has not been 
created yet. Can anyone help me understand this instruction a little 
better? I am averaging an A in the course and would like to keep it.


Below you will find the correspondence between the instructor and 
myself. ( Written Last to First)


Thanks,
Jim

Hello Ms. H,
I am still having trouble with using two text files. I cannot find any 
examples in Cha.7 text book or Code Examples. Would you please point me 
in the direction where I may find an example for this requirement?

Thank You,
Jim


Hello Ms. H,

The sequence of events that I am using now is to open a program that 
will gather gallons used from user, call a module to make calculations, 
use the info from the module to write info to a file named 
FinalProjectBill.txt.


After that is done, I open another program that will read the info 
written to FinalProjectBill.txt


I guess my confusion about the instructions is, if I should read from 
one file FinalProjectBill.txt first how does data get written to that 
file to begin with?


Thank you for the great help.

Jim


From Instructor:
You should read from one file (FinalProjectBill.txt) and then write to 
one (BranleyFinal.txt) with the results as we are learning file i/o 
(input/output) -


The screen is fine to display results also, but in the real world, often 
times all processing is done by reading and writing to files (actually 
databases that are beyond the scope of this class) and nothing ever 
displays on the screen.


You should not write to your input file.

Make sense?

From: Me
Sent: Tuesday, June 26, 2012 5:54 AM
To: Instructor
Subject: Two txt files

Hello Ms. H,

I started writing the program yesterday and by the evening I had

 a WaterPayModule, an InputProgram, an OutputProgram and a text file 
named FinalProjectBill.txt.
When I run the  OutputProgram it gets input from the user, sends info to 
the WaterModule and receives some values that I use to write output to 
the hard drive file FinalProjectBill.txt.
When I run the InputProgram it reads input from the hard drive text file 
FinalProjectBill.txt and prints it on the screen.


My question is why do I need two text files FinalProjectBill.txt and 
BranleyJamesFinal.txt ?


Thank you,
Jim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor