Personal tools
You are here: Home → Comp 171 Scripting Practicum → Course Materials → Python Materials → Python Programs → File Access From Python
Document Actions

File Access From Python

by Dr. William L. Honig — last modified 2012-01-31 10:38 AM

The basics of open, read, write, and close a file

Click here to get the file

Size 1 kB - File type text/python-source

File 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")
    
    

    
« April 2026 »
Su Mo Tu We Th Fr Sa
1234
567891011
12131415161718
19202122232425
2627282930
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: