EditPad Pro User’s Guide
5 stars based on
What will we cover? How to open a file How to read and write to an open file How to close a file. Building an address book Handling binary data files Random Access to file data. Handling files often poses problems for beginners although the reason for this puzzles me slightly. Files in a programming sense are really not very different from files that you use in a word processor or other application: The biggest differences are that in a program you access the file sequentiallythat is, you read one line at a time starting at the beginning.
In practice the word processor often does the same, it just holds the entire file in memory while you work on it and then writes it all back out when you close it. The other difference is that, when programming, you normally open the file as read only or write only. You can write by creating a new file from scratch or overwriting an existing one or by appending to an existing one. One other thing you can do while processing a file is that you can go back to the beginning. Files - Input and Output Let's see that in practice.
We will assume that a file exists called menu. The first is the filename which may be passed as a variable or a literal string, as we did here.
The second is the mode. The mode determines whether we are opening the file for reading r or writing wand also whether it's for text or binary usage - by adding a 'b' to the 'r' or 'w', as in: We read and close the file using functions preceded by the file variable. This read and write binary file in vbscript recall list is known as method invocation and is another example of Object Orientation.
Don't worry about it for now, except to realize that it's related in some ways to modules. You can, if it helps, think of a file variable as being a reference to a module containing functions that operate on files and which we automatically import every time we create a file type variable.
We close the file at the end with the close method. In Python, files are automatically closed at the end of the program but it is good practice to get into the habit of closing read and write binary file in vbscript recall list files explicitly.
Well, the operating system may not write the data out to the file until it is closed this can boost performance. What this means is that if the program exits unexpectedly there is a danger that your precious data may not have been written to the file! So the moral is: We have not specified the full path to the file in the code above so the file will be treated as being in the current folder.
However we can pass a full path name to open instead of just the file name. Now, consider how you could cope with long files. You couldn't display all of the file on a single screen so we need to pause after each screenful of text. You might like to try that as an exercise Another way of reading a file is to use a while loop and a method of the file object called readline. The advantage of this is that we can stop processing the file as soon as we find the data we need, this can greatly speed things up if processing long files.
However it is a little bit more complex, so lets look at the previous example using a while loop: Thereafter we printed each line, read the next and so long as it was not empty went around again. Finally, after exiting the while loop we closed the file. If we wanted to stop at a certain point in the file we would have introduced a branch condition inside the while loop and if it detected the stop condition we simply set the line value to an empty string '' so that the loop will terminate Recall that an empty value is treated as a False boolean value in Python tests.
Really that's all there is to it. You open the file, read it in and manipulate it any way you want to. When you're finished you close the file. However there is one little niggle you may have noticed in the previous example: To avoid that Python provides a string method called strip which will remove whitespaceor non-printable characters, from both ends of a string.
It has cousins which can strip one end only called rstrip and lstrip too If we substitute the print line above with: To create a 'copy' command in Python, we simply open a new file in write mode and write the lines to that file instead of printing them. Create the equivalent of: This kind of user feedback is usually a good idea. Because we wrote out the same read and write binary file in vbscript recall list that we read in there was no problems with newline characters read and write binary file in vbscript recall list. But if we read and write binary file in vbscript recall list been writing out strings which we created, or which we had strip ped earlier we would have needed read and write binary file in vbscript recall list add a newline to the end of the output string, like this: Instead of simply copying the menu we will add todays date to the top.
That way we can easily generate a daily menu from the easily modified text file of meals. All we need to do is write out a couple of lines at the top of the new file before copying the menu. Managing the creation and removal of newline characters is one of the more irritating aspects of handling text files. Operating systems handle files in different ways. This introduces some niggles into our programs if we want them to work on multiple operating systems.
There are two niggles in particular which can catch people out and we'll look at them here:. The whole subject of newlines and text files is a murky area of non standard implementation by different operating systems. These differences have their roots in the early days of data communications and the control of mechanical teleprinters.
Basically there are 3 different ways to indicate a new line: All three techniques are used in different operating systems. Unix including Linux uses method 2. So how can the poor programmer cope with this multiplicity of line endings? In many languages she just has to do lots of tests and take different action per OS.
In more modern languages, including Python, the language provides facilities for dealing with the mess for you. In the case of Python the assistance comes in the form of the os module which defines a variable called linesep which is set to whatever the newline character is on the current operating system. This makes adding newlines easy, and rstrip takes account of the OS when it does its work of removing them, so really the simple way to stay sane, so far as newlines are concerned is: That still leaves the awkward situation where a file is created on one OS and then processed on another, incompatible, OS and sadly, there isn't much we can do about that except to compare the end of the line with os.
This is more of an issue for Windows users than others although MacOS 9 users may bump into it occasionally too. As above each OS specifies paths to files using different characters to separate the drives, folders and files. The generic solution for this is again to use the os module which provides the os. In practice you won't need to use this very often since the path will likely be different for every machine read and write binary file in vbscript recall list So instead you will just enter the full path directly in a string, possibly once for each OS you are running on.
But there is one big gotcha hiding in wait for Windows users That is it takes two characters and treats them as one.
So how do we get round this inconvenience? There are three solutions: This tells Python to ignore any back slashes and treat it as a "raw" sting. This has the added advantage of making your code portable to other operating systems too. Thus any of the following will open our data file correctly: One way to do that would be to open the file for input, read the data into a list, append the data to the list and then write the whole list out to a new version of the old file. If the file is short that's not a problem but if the file is very large, maybe over Mb, then you could run out of memory to hold the list.
Fortunately there's another mode "a" that we can pass to open which allows us to append directly to an existing file just by writing.
Even better, if the file doesn't exist it will open a new file just as if you'd specified "w". Read and write binary file in vbscript recall list an example, let's assume we have a log file that we use for capturing error messages. We don't want to delete the existing messages so we choose to append the error, like this: A common technique is to create a filename based on the date, thus read and write binary file in vbscript recall list the date changes we automatically create a new file and it is easy for the maintainers of the system to find the errors for a particular day and to archive away old error files if they are not needed.
Remember, from the menu example above, that the time module can be used to find out the current date. Python read and write binary file in vbscript recall list has introduced a new, more convenient, way of working with files, particularly when iterating over their contents.
This uses a new construct known as with. It looks like this: With guarantees to close the file at the end of the with statement. This construct makes file handling a little bit more reliable and is read and write binary file in vbscript recall list recommended way of opening files in Python v3. The Address Book Revisited You remember the address book program we introduced during the Raw Materials topic and then expanded in the Talking to the User topic?
Let's start to make it really useful by saving it to a file and, of course, reading the file at startup. We'll do this by writing some functions. So in this example we pull together several of the read and write binary file in vbscript recall list that we've covered in the last few topics. The basic design will require a function to read the file at startup, another to write the file at the end of the program.
We will also create a function to present the user with a menu of options and a separate function for each menu selection. The menu will allow the user to: We import the os module which we use to check that the file path actually exists before opening the file.
We defined the filename as a module level variable so we can use it both in loading and saving the data. We use rstrip to remove the new-line character from the end of the line.