Python odds and ends

 from sys import argv unpack argv

a,b,c…=argv

> python file.py ‘b’,’c’,…

argv will be a list contains

[‘file.py’, ‘a’,’b’,…]

a = python file name itself

b,c, …. is the variables pass when exec the python file

 The argument mode points to a string beginning with one of the following
 sequences (Additional characters may follow these sequences.):

 ``r''   Open text file for reading.  The stream is positioned at the
         beginning of the file.

 ``r+''  Open for reading and writing.  The stream is positioned at the
         beginning of the file.

 ``w''   Truncate file to zero length or create text file for writing.
         The stream is positioned at the beginning of the file.

 ``w+''  Open for reading and writing.  The file is created if it does not
         exist, otherwise it is truncated.  The stream is positioned at
         the beginning of the file.

 ``a''   Open for writing.  The file is created if it does not exist.  The
         stream is positioned at the end of the file.  Subsequent writes
         to the file will always end up at the then current end of file,
         irrespective of any intervening fseek(3) or similar.

 ``a+''  Open for reading and writing.  The file is created if it does not
         exist.  The stream is positioned at the end of the file.  Subse-
         quent writes to the file will always end up at the then current
         end of file, irrespective of any intervening fseek(3) or similar.
                  | r   r+   w   w+   a   a+
------------------|--------------------------
read              | +   +        +        +
write             |     +    +   +    +   +
create            |          +   +    +   +
truncate          |          +   +
position at start | +   +    +   +
position at end   |                   +   +

where meanings are: (just to avoid any misinterpretation)

  • read – reading from file is allowed
  • write – writing to file is allowed
  • create – file is created if it does not exist yet
  • trunctate – during opening of the file it is made empty (all content of the file is erased)
  • position at start – after file is opened, initial position is set to the start of the file
  • position at end – after file is opened, initial position is set to the end of the file
  • close — Closes the file. Like File->Save.. in your editor.
  • read — Reads the contents of the file. You can assign the result to a variable.
  • readline — Reads just one line of a text file.
  • truncate — Empties the file. Watch out if you care about the file.
  • write('stuff') — Writes “stuff” to the file.
 file manipulate manipulate.py:

from sys import argv

script, input_file = argv

current_file = open(input_file)

def print_all(f):
print f.read()

def rewind(f):
f.seek(0)

def print_a_line(line_count, f):
print line_count, f.readline()

 $ python manipulate.py test.txt

Why does seek(0) not set the current_line to 0?
First, the seek() function is dealing in bytes, not lines. The code seek(0) moves the file to the 0 byte (first byte) in the file. Second, current_line is just a variable and has no real connection to the file at all. We are manually incrementing it.

 

How does readline() know where each line is?
Inside readline() is code that scans each byte of the file until it finds a \n character, then stops reading the file to return what it found so far. The file f is responsible for maintaining the current position in the file after each readline() call, so that it will keep reading each line.