Chapter III
Getting interactive
OBJECTIVES
Get user input from keyboard
Read and Write disk files
Application Logging
Basic I/O
The first time you run python in the command line you will get a prompt that will ask you to type python code that will be executed once you hit Enter key. This input mode is the most used in case of command line interactive programs. The interaction consists in waiting, accepting and processing user input then taking the corresponding actions. One of the actions should be the exit or quit command that will allow graceful termination of your program.
We will implement a command line program that will implement the following commands:
exit - gracefully terminate the program
time - display the current date and time on the machine
help - display program usage details
File I/O
One of the most basic functionalities of an Operating System is the file system. In fact most of the Input/Output (I/O) activity is centered around files: stored on the physical disk, as input or output physical devices, as network sockets, in memory or as primitives for Inter Process Communication (locks, shared access, synchronization etc). We will start with the simplest form of file I/O: disk stored files used for sequential read or write operations, information stored in plain text format.
Because files are protected resources, you need to have access to them in order to perform specific operations on them: you need read and/or write access to the file. This is managed by the Operating System itself and differs from one OS to another. The safest supposition is to say that you have full access to the folder where your Python file is stored: Chapter_III
and that you can perform all operations here. In general the user that will run the Python script must have the corresponding access rights to the location.
We will have to perform the following operations with files:
create a new file for writing
open an existing file for reading
It is good practice to use the with
keyword when dealing with file objects. The advantage is that the file is properly closed after its suite finishes, even if an exception is raised at some point. Using with
is also much shorter than writing equivalent try-finally
blocks.
Application logging
One special type of file is the application log file. This is the most useful tool for the programmer. Almost all of the applications that you will write as a professional will run either on a server or on client's machine - you will not have direct access to that machine. Almost all software fails, has bugs and raises errors. The only mean by which you will be capable to know what happened, when it happened and the execution context is to log the activity of your application, not only when it crushes but also when it performs correctly. In general an error occurs starting from a valid, legitimate state! Please record that state.
A log file is a text file containing messages that are either generated by the application, environment, the OS or errors, warning messages. Most of the programming languages have built in logging libraries, use them as a professional! Also find a tool to gather and process those log files - they contain precious information.
The solution
The requirements for this assignment:
write a Python program that will allow the user to specify an input file,
the program should be able to read the file, line by line. The input file is in text format.
the application should log the activity in a file called
file_io.log
:application start
command line input parameters
line read from the file
number of Ms/Mrs/Mr read
application end
What have you learned:
how to perform basic i/o operations and interact with the user
how to read and write to a file
how to use logging in order to debug and follow program execution
Homework
add logging to basic i/o application
Last updated
Was this helpful?