473,287 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

[perl-python] 20050121 file reading & writing

# -*- 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 pe*******************@yahoogroups.com
if you are reading it on a web page, program examples may not run
because html conversion often breaks the code.
Xah
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html

Jul 18 '05 #1
4 3019
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/
Jul 18 '05 #2
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 && ma*@alcyone.com && 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
Jul 18 '05 #3
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
Jul 18 '05 #4
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>

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark Wilson CPU | last post by:
This must be easy, but I'm missing something... I want to execute a Perl script, and capture ALL its output into a PHP variable. Here are my 2 files: -------------------------------------...
2
by: noone | last post by:
Hello all, I am absolutely brand new to perl, but I understand that it is a very important aspect of web development to create dynamic web pages. I am kind of short on money, being a college...
3
by: David F. Skoll | last post by:
Hi, I'm tearing my hair out on this one. I'm trying to embed a Perl interpreter into a C program. I need to be able to create and destroy the interpreter periodically, but will never actually...
1
by: Julia Bell | last post by:
I would like to run the same script on two different platforms. The directory in which the script(s) will be stored is common to the two platforms. (I see the same directory contents regardless...
4
by: Firewalker | last post by:
Hey guys, I am newbie to perl. I am trying to deal with dates ( such trying to find what the date would be after month). Is therea function or date class( I am a java programmer, I couldnt find...
6
by: surfivor | last post by:
I may be involved in a data migration project involving databases and creating XML feeds. Our site is PHP based, so I imagine the team might suggest PHP, but I had a look at the PHP documentation...
4
by: billb | last post by:
I installed a perl extension for PHP to use some perl inside my php primarily because I have perl working with oracle and not php and oracle. So I want to use my old perl scripts, and use the...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
223
by: Pilcrow | last post by:
Given that UNIX, including networking, is almost entirely coded in C, how come so many things are almost impossible in ordinary C? Examples: Network and internet access, access to UNIX...
4
by: vijayarl | last post by:
Hi All, i have the following software installed in my system : 1.OS: Win2k 2.Eclipse Version used :3.4.0 & even the perl too... 1. I have imported the my own perl project in Eclipse, when i...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.