Writestrin note the number has been converted into

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: vent the overwriWng data in an exisWng file, test to see if the file exists or not. import os.path if os.path.isfile(“names.txt”): print(“names.txt exists”) •  The isfile(“names.txt”) returns True if the file names.txt exists in the current directory, and False otherwise. CMPUT174  ­ ­ References & Files 9 3 13 ­10 ­21 Wri9ng to a File • You can write to a file by using the write func9on. outfile.write(“Jupiter \n”) outfile.write(“Nepture\n”) outfile.write(“Pluto\n”) • The write func9on does not insert a newline character automa9cally. CMPUT174  ­ ­ References & Files 10 Wri9ng Numeric Data • Numbers must be converted to strings before they can be wriben into a file outfile.open(“Numbers.txt”,”w”) for i in range(1,10): outfile.write(str(i)+"\n") NOTE: The number has been converted into a string before wriWng to the file. CMPUT174  ­ ­ References & Files 11 Example of reading and wri9ng ...
View Full Document

This document was uploaded on 03/02/2014.

Ask a homework question - tutors are online