Txtw if the le does not exist open creates a new le be

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview:  ­ References & Files 6 2 13 ­10 ­21 Closing a file • To use a file, we first need to “open” it • It is good pracWce to always “close” a file when we are done with it • Forge]ng to close a file is ok someWmes, but may cause problems, especially if you are wriWng to a file • To close a file: infile.close() CMPUT174  ­ ­ References & Files 7 Wri9ng to a file • First, open the file for wriWng by using the open command with the mode “w” outfile = open(“names.txt”,”w”) • If the file does not exist, open creates a new file. • Be careful: if the file already exists, then the contents of the file is overwriEen by new data • You can check whether the file exists or not (example on next slide) CMPUT174  ­ ­ References & Files 8 Tes9ng a File Existence • To pre...
View Full Document

This document was uploaded on 03/02/2014.

Ask a homework question - tutors are online