Perform IO with Text Files

From rbachwiki
Jump to navigation Jump to search

Opening a File

myfile= open('filename.txt') # reads a string of everything in the file # reset the cursor to the top of the file
contents =
myfile.readlines() # will read lines as a list
myfile.close() # closes the file
with open('filename.txt') as my_new_file:
 contents =

Open ReadOnly

with open('myfile.txt', mode= 'r') as myfile:
 contents =

Available Modes:

mode-'r' Read only
mode='w' write only (will overwrite files or create new)
mode= 'a' append only (will add to a file)
mode = 'r+' reading and writing
mode= 'w+' is writing and reading (Overwrites existing files or crates a new file)

Back To Top - Category