[perl-python] 20050121 file reading & writing

From: Xah Lee (xah_at_xahlee.org)
Date: 01/22/05


Date: 22 Jan 2005 03:57:40 -0800


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



Relevant Pages

  • Re: Python is fun (useless social thread) ;-)
    ... quite frankly perl's a ridiculous language. ... perl had already poisoned my mind against the syntax... ... Programming, reading this newsgroup, reading the python cookbook, ...
    (comp.lang.python)
  • Re: Scripting question, [small programming question
    ... On Wednesday 25 May 2005 9:11 am, Les Mikesell wrote: ... > comprehensive language like perl. ... As to your comments above, Les, I have been doing some research and reading, ... and I bought one yesterday to introduce myself to Python. ...
    (Fedora)
  • Re: Scripting question, [small programming question
    ... I'm currently reading a book on ... and I bought one yesterday to introduce myself to Python. ... > advocates show for Perl. ... Les Mikesell ...
    (Fedora)
  • [perl-python] 20050121 file reading & writing
    ... # reading the one line ... # proper in large programs. ... # or in Python terminal, ... # in perl, similar functionality exists. ...
    (comp.lang.perl.misc)
  • Re: Perl 5.8.0
    ... I don't know about the others, but my mind reading skills are minimal and ... Cannot find the core of Perl library. ... Everything else lives in the \perl tree. ... are in PATH or LIBPATH. ...
    (comp.os.os2.programmer.tools)