File Access From Python
The basics of open, read, write, and close a file
Size 1 kB - File type text/python-sourceFile contents
#
#Comp 171 Standard Python Script Header
#Assignment name: Simple Read and Write of Files
#LUC id and Name: whonig@luc.edu Dr. William L. Honig
#Date 24 Jan 12
#
#The basics of reading and writing text files
#
#Questions: What else would you like to do with files????
#
#
def writeMyFile():
myFile = open("simpleFile.txt", "a")
myFile.write("Here is the first line of the file!\n")
myFile.write("I want this to be the second line of the file.\n")
myFile.close()
def readMyFile():
myFile = open("simpleFile.txt", "r")
fileInfo = myFile.read()
print(fileInfo)
myFile.close()
def readLineByLine():
myFile = open("simpleFile.txt", "r")
for line in myFile:
print(line, end="")
print("THAT'S ALL FOLKS!")
myFile.close()
def deleteMyFile():
import os
os.remove("simpleFile.txt")
print("It's Gone")
Click here to get the file