Main.Files History
Hide minor edits - Show changes to markup
Reading and writing files in Python - the open command
Reading files in Python
- an alternative:
line = file_handle.next()
- let's do this a few more times until there is nothing left:
line = file_handle.next() line = file_handle.next()
- A file object has all the features of an iterator:
- a 'next()' method and raises a StopIteration
- exception when it no longer has a next element
- therefore we can use it with a for loop:
- note that the command
- now let's extract the numbers in the file into a list:
numbers = []
- can also be written as:
file_handle = open('numbers.txt', 'r')
- the additional argument tells python that we want to open the file for
- the purpose of reading from it (the mode in which to open the file).
- 'r' is the default mode.
- if you want to write to a file simply open it (it doesn't need to exist)
- 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')
- note that we needed to explicitly put in a new-line character.
file_handle.write('now it has two lines\n')
- and close the file when done:
file_handle.close()
- 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()
- note that the write method of a file takes a string as its parameter.
- if you want to write a floating point number or integer you need to
- 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()
for line in file_handle :
numbers += [int(line)]
(:source lang=python:)
""" Reading and writing files in Python - the open command """
- in order to read from a file you first need to open it:
file_handle = open('numbers.txt')
- you can obtain the next line in the file using its readline method
line = file_handle.readline()
- an alternative:
line = file_handle.next()
- let's do this a few more times until there is nothing left:
line = file_handle.next() line = file_handle.next()
- A file object has all the features of an iterator:
- a 'next()' method and raises a StopIteration
- exception when it no longer has a next element
- therefore we can use it with a for loop:
file_handle = open('numbers.txt') for line in file_handle :
print line
- once we're done with a file we close it:
file_handle.close()
- It is a good idea to check whether a file exists before attempting
- to open it:
import os os.path.exists('numbers.txt')
- note that the command
file_handle = open('numbers.txt')
- can also be written as:
file_handle = open('numbers.txt', 'r')
- the additional argument tells python that we want to open the file for
- the purpose of reading from it (the mode in which to open the file).
- 'r' is the default mode.
- if you want to write to a file simply open it (it doesn't need to exist)
- 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')
- note that we needed to explicitly put in a new-line character.
file_handle.write('now it has two lines\n')
- and close the file when done:
file_handle.close()
- 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()
- note that the write method of a file takes a string as its parameter.
- if you want to write a floating point number or integer you need to
- 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:)
