Download Programming in Python

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
File handling The sys module
File handling The sys module
Opening files
File handling
I
The call open(filename, mode=’r’) will return a file
object, whose type is file
I
This file object can be used to refer to a file on disk. For
example, when we want to read from or write to a file, we can
use the methods read and write of the file object
I
After the file object is no longer needed, a call to the close
method should be made
The sys module
Jarkko Toivonen (CS Department)
Programming in Python
1 / 13
Jarkko Toivonen (CS Department)
File handling The sys module
File open modes 2
I
We can control what kind of operations we can perform on a
file with the mode parameter of open function
I
Different options include opening a file for reading or write,
whether the file should exists already or be created with the
call to open, etc
I
Here’s a list of all the opening modes:
read-only mode, file must exist
write-only mode, creates, or overwrites an existing file
write-only mode, write always appends to the end
read/write mode, file must already exist
read/write mode, creates, or overwrites an existing file
read/write mode, write will append to end
Jarkko Toivonen (CS Department)
2 / 13
File handling The sys module
File open modes 1
r
w
a
r+
w+
a+
Programming in Python
Programming in Python
3 / 13
I
The mode specification can optionally end with b or t
character, denoting binary and text modes, correspondingly
I
On Unix, there’s no difference between these modes
I
On Windows, when using text mode, the line ending
characters are interpreted as \n. This is supposed to make file
handling more portable
Jarkko Toivonen (CS Department)
Programming in Python
4 / 13
File handling The sys module
File handling The sys module
Some common file object methods
I
read(size) will read size characters as a string from a file
I
write(string) will write string to a file
I
Seeking and telling the position in a file 1
I
Normally all these read and write operations work in
consequtive manner
readline() will read a string until and including the next
newline character is met
I
That is, the next read operation will continue from the file
position where the last read operation finished
I
readlines() will return a list of all lines of a file
I
I
writelines(lst) will write a list of lines to a file
And, the next write operation will continue from the position
where the previous write finished
I
flush() will try to make sure that the changes made to a file
are written to disk immediately
I
This behaviour can be changed with the seek method:
f.seek(pos, how=0)
Jarkko Toivonen (CS Department)
Programming in Python
5 / 13
Jarkko Toivonen (CS Department)
File handling The sys module
The pos argument is an integer that refers to a file position
starting from the reference point how
I
The reference point is either the start of the file (how==0),
current position (how==1), or the end of the file (how==2)
I
Note that the pos parameter can also be negative
I
The method tell will return an integer that tells the current
position from the start of the file
Jarkko Toivonen (CS Department)
Programming in Python
6 / 13
File handling The sys module
Seeking and telling the position in a file 2
I
Programming in Python
Iterating a file object
I
We can iterate through all the lines of a file f using the
following syntax:
for line in f:
# process line
7 / 13
I
The file f needs to be open for text-mode reading
I
This is an efficient way of processing a file
Jarkko Toivonen (CS Department)
Programming in Python
8 / 13
File handling The sys module
File handling The sys module
Standard file objects 1
I
Standard file objects 2
Python has automatically three file objects open:
I
I
I
sys.stdin for standard input
sys.stdout for standard output
sys.stderr for standard error
I
These standard file objects are meant to be a basic
input/output mechanism in textual form
I
To read a line from a user (keyboard), you can call
sys.stdin.readline()
I
The destinations of the file objects can be changed to point
somewhere else than the usual keyboard and monitor
I
To write a line to a user (monitor), call
sys.stdout.write(line)
I
Very often these are redirected to some files
I
For example, it is usual to point the stderr to a file where all
error messages are logged
I
The standard error is meant for error messages only, even
though often its output goes to the same destination as
standard output
Jarkko Toivonen (CS Department)
Programming in Python
9 / 13
Jarkko Toivonen (CS Department)
File handling The sys module
10 / 13
File handling The sys module
Command line arguments to the program
I
Programming in Python
Command line arguments to the program
Let’s say you start your program from the operating system,
for example, with
python progname.py param1 param2 param3
I
The element sys.argv[0] is the name of the program
I
Python will start executing your program progname
I
I
You can access the command line parameters param1,
param2, and param3 from the sys module’s attribute argv,
which is a list
The rest of the elements sys.argv[1:] are the command
line parameters given to the program
I
The command line parameters can be handled manually or by
using some dedicated module, like the optparse module
I
In the previous example the statement print sys.argv
would print
[’progname.py’, ’param1’, ’param2’, ’param3’]
Jarkko Toivonen (CS Department)
Programming in Python
11 / 13
Jarkko Toivonen (CS Department)
Programming in Python
12 / 13
File handling The sys module
Other useful sys module attributes
I
The function sys.exit(value) will stop the execution of the
program and return value to the operating system.
I
Usually this value tell whether the program ended successfully,
or if not then the value means the error code
I
A return value zero means success
I
The attribute sys.path contains a list of all those directories
that will be searched for imported modules
I
The first element of this list is an empty string that denotes
the current directory
Jarkko Toivonen (CS Department)
Programming in Python
13 / 13