Re: Basic file operation questions

2005-02-08 Thread Grant Edwards
On 2005-02-08, Marc Huffnagle <[EMAIL PROTECTED]> wrote: >>>for line in file(...): >>> # do stuff > When you read a file with that method, is there an implied close() call > on the file? I assume there is, but how is that handled? The file will be closed when the the file object is deleted b

Re: Basic file operation questions

2005-02-08 Thread Caleb Hattingh
Marc I don't know how it is handled, but I expect also that there is an implied close(). thanks Caleb When you read a file with that method, is there an implied close() call on the file? I assume there is, but how is that handled? -- http://mail.python.org/mailman/listinfo/python-list

Re: Basic file operation questions

2005-02-08 Thread Jeff Shannon
Marc Huffnagle wrote: When you read a file with that method, is there an implied close() call on the file? I assume there is, but how is that handled? [...] for line in file(...): # do stuff As I understand it, the disk file will be closed when the file object is garbage collected. In CPytho

Re: Basic file operation questions

2005-02-08 Thread Marc Huffnagle
When you read a file with that method, is there an implied close() call on the file? I assume there is, but how is that handled? Caleb Hattingh wrote: Peter, that was very clear, thanks. So not only is for line in file(...): # do stuff the most elegant, it is also the fastest. file.readlines(

Re: Basic file operation questions

2005-02-07 Thread Caleb Hattingh
Peter, that was very clear, thanks. > So not only is > > for line in file(...): ># do stuff > > the most elegant, it is also the fastest. file.readlines() comes close, but > is only viable for "small" files. -- http://mail.python.org/mailman/listinfo/python-list

Re: Basic file operation questions

2005-02-03 Thread Peter Otten
Caleb Hattingh wrote: >> Yes, you can even write >> >> f = open("data.txt") >> for line in f: >> # do stuff with line >> f.close() >> >> This has the additional benefit of not slurping in the entire file at >> once. > > Is there disk access on every iteration? I'm guessing yes? It shouldn'

Re: Basic file operation questions

2005-02-03 Thread Jeff Shannon
Caleb Hattingh wrote: Peter Yes, you can even write f = open("data.txt") for line in f: # do stuff with line f.close() This has the additional benefit of not slurping in the entire file at once. Is there disk access on every iteration? I'm guessing yes? It shouldn't be an issue in the va

Re: Basic file operation questions

2005-02-03 Thread Steven Bethard
Caleb Hattingh wrote: Peter Yes, you can even write f = open("data.txt") for line in f: # do stuff with line f.close() This has the additional benefit of not slurping in the entire file at once. Is there disk access on every iteration? I'm guessing yes? It shouldn't be an issue in the va

Re: Basic file operation questions

2005-02-03 Thread Caleb Hattingh
Peter Yes, you can even write f = open("data.txt") for line in f: # do stuff with line f.close() This has the additional benefit of not slurping in the entire file at once. Is there disk access on every iteration? I'm guessing yes? It shouldn't be an issue in the vast majority of cases,

Re: Basic file operation questions

2005-02-03 Thread Steve Holden
[EMAIL PROTECTED] wrote: In article <[EMAIL PROTECTED]>, Peter Nuttall wrote: On Wed, Feb 02, 2005 at 11:47:41PM -0500, Caleb Hattingh wrote: Hi Alex Assuming you have a file called "data.txt": *** f = open('data.txt','r') lines = f.readlines() f.close() for line in lines: print line *** Can you

Re: Basic file operation questions

2005-02-03 Thread Peter Otten
Peter Nuttall wrote: > On Wed, Feb 02, 2005 at 11:47:41PM -0500, Caleb Hattingh wrote: >> Hi Alex >> >> Assuming you have a file called "data.txt": >> >> *** >> f = open('data.txt','r') >> lines = f.readlines() >> f.close() >> for line in lines: >> print line >> *** >> > > Can you not write

Re: Basic file operation questions

2005-02-03 Thread Michael . Lang
In article <[EMAIL PROTECTED]>, Peter Nuttall wrote: > On Wed, Feb 02, 2005 at 11:47:41PM -0500, Caleb Hattingh wrote: >> Hi Alex >> >> Assuming you have a file called "data.txt": >> >> *** >> f = open('data.txt','r') >> lines = f.readlines() >> f.close() >> for line in lines: >> print line >

Re: Basic file operation questions

2005-02-03 Thread Peter Nuttall
On Wed, Feb 02, 2005 at 11:47:41PM -0500, Caleb Hattingh wrote: > Hi Alex > > Assuming you have a file called "data.txt": > > *** > f = open('data.txt','r') > lines = f.readlines() > f.close() > for line in lines: > print line > *** > Can you not write this: f=open("data.txt", "r") for line

Re: Basic file operation questions

2005-02-02 Thread David Douard
David Douard wrote: > Marcel van den Dungen wrote: > >> alex wrote: >>> Hi, >>> >>> I am a beginner with python and here is my first question: >>> How can I read the contents of a file using a loop or something? I open >>> the file with file=open(filename, 'r') and what to do then? Can I use >>>

Re: Basic file operation questions

2005-02-02 Thread David Douard
Marcel van den Dungen wrote: > alex wrote: >> Hi, >> >> I am a beginner with python and here is my first question: >> How can I read the contents of a file using a loop or something? I open >> the file with file=open(filename, 'r') and what to do then? Can I use >> something like >> >> for xxx i

Re: Basic file operation questions

2005-02-02 Thread Steve Holden
alex wrote: Hi, I am a beginner with python and here is my first question: How can I read the contents of a file using a loop or something? I open the file with file=open(filename, 'r') and what to do then? Can I use something like for xxx in file: Yes, indeed you can. That's by no means *a

Re: Basic file operation questions

2005-02-02 Thread Marcel van den Dungen
alex wrote: Hi, I am a beginner with python and here is my first question: How can I read the contents of a file using a loop or something? I open the file with file=open(filename, 'r') and what to do then? Can I use something like for xxx in file: Thanks for help Alex take a look at this:

Re: Basic file operation questions

2005-02-02 Thread Caleb Hattingh
Hi Alex Assuming you have a file called "data.txt": *** f = open('data.txt','r') lines = f.readlines() f.close() for line in lines: print line *** Will print each line of the file. You can make a huge investment by setting 2 or 3 hours aside to go through the Python tutorial, which gets insta

Basic file operation questions

2005-02-02 Thread alex
Hi, I am a beginner with python and here is my first question: How can I read the contents of a file using a loop or something? I open the file with file=open(filename, 'r') and what to do then? Can I use something like for xxx in file: Thanks for help Alex -- http://mail.python.org/m