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

    
« October 2027
Su Mo Tu We Th Fr Sa
12
3456789
10111213141516
17181920212223
24252627282930
31
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: