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

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.