Download File handling, the sys module

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

* Your assessment is very important for improving the work of 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
Jarkko Toivonen (CS Department)
Programming in Python
1
File handling The sys module
Opening files
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
Jarkko Toivonen (CS Department)
Programming in Python
2
File handling The sys module
File open modes 1
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:
r
w
a
r+
w+
a+
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)
Programming in Python
3
File handling The sys module
File open modes 2
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
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
readline() will read a string until and including the next
newline character is met
I
readlines() will return a list of all lines of a file
I
writelines(lst) will write a list of lines to a file
I
flush() will try to make sure that the changes made to a file
are written to disk immediately
Jarkko Toivonen (CS Department)
Programming in Python
5
File handling The sys module
Seeking and telling the position in a file 1
I
Normally all these read and write operations work in
consequtive manner
I
That is, the next read operation will continue from the file
position where the last read operation finished
I
And, the next write operation will continue from the position
where the previous write finished
I
This behaviour can be changed with the seek method:
f.seek(pos, how=0)
Jarkko Toivonen (CS Department)
Programming in Python
6
File handling The sys module
Seeking and telling the position in a file 2
I
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
7
File handling The sys module
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
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
File handling The sys module
Standard file objects 1
I
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
To read a line from a user (keyboard), you can call
sys.stdin.readline()
I
To write a line to a user (monitor), call
sys.stdout.write(line)
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
File handling The sys module
Standard file objects 2
I
These standard file objects are meant to be a basic
input/output mechanism in textual form
I
The destinations of the file objects can be changed to point
somewhere else than the usual keyboard and monitor
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
Jarkko Toivonen (CS Department)
Programming in Python
10
File handling The sys module
Command line arguments to the program
I
Let’s say you start your program from the operating system,
for example, with
python progname.py param1 param2 param3
I
Python will start executing your program progname
I
You can access the command line parameters param1,
param2, and param3 from the sys module’s attribute argv,
which is a list
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
File handling The sys module
Command line arguments to the program
I
The element sys.argv[0] is the name of the program
I
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
Jarkko Toivonen (CS Department)
Programming in Python
12
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