Main.Files History

Hide minor edits - Show changes to markup

February 27, 2013, at 02:04 PM MST by 129.82.44.223 -
Changed line 4 from:

Reading and writing files in Python - the open command

to:

Reading files in Python

Changed lines 13-23 from:
  1. an alternative:

line = file_handle.next()

  1. let's do this a few more times until there is nothing left:

line = file_handle.next() line = file_handle.next()

  1. A file object has all the features of an iterator:
  2. a 'next()' method and raises a StopIteration
  3. exception when it no longer has a next element
  4. therefore we can use it with a for loop:
to:
Changed lines 26-28 from:
  1. note that the command
to:
  1. now let's extract the numbers in the file into a list:

numbers = []

Changed lines 30-56 from:
  1. can also be written as:

file_handle = open('numbers.txt', 'r')

  1. the additional argument tells python that we want to open the file for
  2. the purpose of reading from it (the mode in which to open the file).
  3. 'r' is the default mode.
  4. if you want to write to a file simply open it (it doesn't need to exist)
  5. with the 'w' mode (w stands for write):

file_handle = open('my_new_file.txt', 'w') file_handle.write('this file now has one line\n')

  1. note that we needed to explicitly put in a new-line character.

file_handle.write('now it has two lines\n')

  1. and close the file when done:

file_handle.close()

  1. If we remember we want to add stuff to a file open it in append mode ('a'):

file_handle = open('my_new_file.txt', 'a') file_handle.write('we just added another line\n') file_handle.close()

  1. note that the write method of a file takes a string as its parameter.
  2. if you want to write a floating point number or integer you need to
  3. convert it to a string first:

file_handle = open('my_new_file.txt', 'a') file_handle.write(str(1.1) + '\n') file_handle.write('%2.1f %2.1f' % (15.3333, 20.289) + '\n') file_handle.close()

to:

for line in file_handle :

    numbers += [int(line)]
February 21, 2010, at 07:40 PM MST by 71.196.160.210 -
Added lines 1-66:

(:source lang=python:)

""" Reading and writing files in Python - the open command """

  1. in order to read from a file you first need to open it:

file_handle = open('numbers.txt')

  1. you can obtain the next line in the file using its readline method

line = file_handle.readline()

  1. an alternative:

line = file_handle.next()

  1. let's do this a few more times until there is nothing left:

line = file_handle.next() line = file_handle.next()

  1. A file object has all the features of an iterator:
  2. a 'next()' method and raises a StopIteration
  3. exception when it no longer has a next element
  4. therefore we can use it with a for loop:

file_handle = open('numbers.txt') for line in file_handle :

    print line
  1. once we're done with a file we close it:

file_handle.close()

  1. It is a good idea to check whether a file exists before attempting
  2. to open it:

import os os.path.exists('numbers.txt')

  1. note that the command

file_handle = open('numbers.txt')

  1. can also be written as:

file_handle = open('numbers.txt', 'r')

  1. the additional argument tells python that we want to open the file for
  2. the purpose of reading from it (the mode in which to open the file).
  3. 'r' is the default mode.
  4. if you want to write to a file simply open it (it doesn't need to exist)
  5. with the 'w' mode (w stands for write):

file_handle = open('my_new_file.txt', 'w') file_handle.write('this file now has one line\n')

  1. note that we needed to explicitly put in a new-line character.

file_handle.write('now it has two lines\n')

  1. and close the file when done:

file_handle.close()

  1. If we remember we want to add stuff to a file open it in append mode ('a'):

file_handle = open('my_new_file.txt', 'a') file_handle.write('we just added another line\n') file_handle.close()

  1. note that the write method of a file takes a string as its parameter.
  2. if you want to write a floating point number or integer you need to
  3. convert it to a string first:

file_handle = open('my_new_file.txt', 'a') file_handle.write(str(1.1) + '\n') file_handle.write('%2.1f %2.1f' % (15.3333, 20.289) + '\n') file_handle.close()

(:sourceend:)