Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [perl-python] 20050121 file reading & writing

Reply
Thread Tools

[perl-python] 20050121 file reading & writing

 
 
Xah Lee
Guest
Posts: n/a
 
      01-22-2005
# -*- coding: utf-8 -*-
# Python

# to open a file and write to file
# do

f=open('xfile.txt','w')
# this creates a file "object" and name it f.

# the second argument of open can be
# 'w' for write (overwrite exsiting file)
# 'a' for append (ditto)
# 'r' or read only


# to actually print to file or read from
# file, one uses methods of file
# objects. e.g.

# reading entire file
# text = f.read()

# reading the one line
# line = f.realine()

# reading entire file as a list, of lines
# mylist = f.readlines()

# to write to file, do
f.write('yay, first line!\n')

# when you are done, close the file
f.close()

# closing files saves memory and is
# proper in large programs.

# see
# http://python.org/doc/2.3.4/tut/node9.html

# or in Python terminal,
# type help() then topic FILES

# try to write a program that read in a
# file and print it to a new file.

------------------------
# in perl, similar functionality exists.
# their construct is quite varied.

# example of reading in file
# and print it out
# (first, save this file as x.pl)
open(f,"<x.pl") or die "error: $!";
while ($line = <f>) {print $line}
close(f) or die "error: $!";
print "am printing myself\n";

# the above is a so called "idiom"
# meaning that it is the way such is
# done in a particular language, as in
# English.

# note, the f really should be F in Perl
# by some references, but can also be
# lower case f or even "f". All are not
# uncommon. There is no clear reason for
# why or what should be or what
# is the difference. Usually it's
# not worthwhile to question in
# Perl. ">x.pl" would be for write to
# file. The <f> tells perl the file
# object, and when Perl sees t=<> it
# reads a line. (usually, but technically
# depending on some predefined
# variables...) The f they call "file handle".
# ... see
# perldoc -tf open
# to begin understanding.

------------
Note: this post is from the Perl-Python a-day mailing list at
http://groups.yahoo.com/group/perl-python/
to subscribe, send an email to http://www.velocityreviews.com/forums/(E-Mail Removed)
if you are reading it on a web page, program examples may not run
because html conversion often breaks the code.
Xah
(E-Mail Removed)
http://xahlee.org/PageTwo_dir/more.html

 
Reply With Quote
 
 
 
 
Gian Mario Tagliaretti
Guest
Posts: n/a
 
      01-22-2005
Xah Lee wrote:

> # the second argument of open can be
> # 'w' for write (overwrite exsiting file)
> # 'a' for append (ditto)
> # 'r' or read only


are you sure you didn't forget something?

> # reading the one line
> # line = f.realine()


wrong

> [...]


Maybe you didn't get the fact the you won't see a flame starting between
python people and perl friends?

throw yourself somewhere and... Xah.flush()
--
Gian Mario Tagliaretti
PyGTK GUI programming
http://www.parafernalia.org/pygtk/
 
Reply With Quote
 
 
 
 
Bob Smith
Guest
Posts: n/a
 
      01-22-2005
Xah Lee wrote:

> # reading entire file as a list, of lines
> # mylist = f.readlines()



To do this efficiently on a large file (dozens or hundreds of megs), you
should use the 'sizehint' parameter so as not to use too much memory:

sizehint = 0
mylist = f.readlines(sizehint)



 
Reply With Quote
 
Erik Max Francis
Guest
Posts: n/a
 
      01-23-2005
Bob Smith wrote:

> To do this efficiently on a large file (dozens or hundreds of megs), you
> should use the 'sizehint' parameter so as not to use too much memory:
>
> sizehint = 0
> mylist = f.readlines(sizehint)


It doesn't make any difference. .readlines reads the entire file into
memory at once.

--
Erik Max Francis && (E-Mail Removed) && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Can I lay with you / As your wife
-- India Arie
 
Reply With Quote
 
Bob Smith
Guest
Posts: n/a
 
      01-23-2005
Erik Max Francis wrote:
> Bob Smith wrote:
>
>> To do this efficiently on a large file (dozens or hundreds of megs),
>> you should use the 'sizehint' parameter so as not to use too much memory:
>>
>> sizehint = 0
>> mylist = f.readlines(sizehint)

>
>
> It doesn't make any difference. .readlines reads the entire file into
> memory at once.
>


Are you sure, the docs say this:

"f.readlines() returns a list containing all the lines of data in the
file. If given an optional parameter sizehint, it reads that many bytes
from the file and enough more to complete a line, and returns the lines
from that. This is often used to allow efficient reading of a large file
by lines, but without having to load the entire file in memory. Only
complete lines will be returned."

http://docs.python.org/tut/node9.html
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      01-23-2005
Erik Max Francis wrote:

>> To do this efficiently on a large file (dozens or hundreds of megs), you should use the
>> 'sizehint' parameter so as not to use too much memory:
>>
>> sizehint = 0
>> mylist = f.readlines(sizehint)

>
> It doesn't make any difference. .readlines reads the entire file into memory at once.


except when it doesn't:

readlines([sizehint])

Read until EOF using readline() and return a list containing the
lines thus read. If the optional sizehint argument is present, instead
of reading up to EOF, whole lines totalling approximately sizehint
bytes (possibly after rounding up to an internal buffer size) are read.
Objects implementing a file-like interface may choose to ignore
sizehint if it cannot be implemented, or cannot be implemented
efficiently.

>>> f = open("ot.xml")
>>> s = f.readlines(1000)
>>> len(s)

157

>>> f = open("ot.xml")
>>> s = f.readlines()
>>> len(s)

48560

</F>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating Applet reading/writing to file michael.rygh@gmail.com Java 8 12-17-2005 12:25 AM
[perl-python] 20050121 file reading & writing Xah Lee Perl Misc 2 01-23-2005 06:27 PM
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM
Newbie needs a tutorial on reading and writing to nodes in an XML file David Hearn ASP .Net 1 06-30-2004 12:58 PM
mix mode file reading/writing? john smith C++ 4 07-30-2003 01:05 PM



Advertisments