FileIO_FunctionalProgramming

FileIO_FunctionalProgramming - CS 1301 File I/O Reference...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 1301 File I/O Reference Sheet Opening a file <variable>=open(<“filename”>,<“mode”>) Three modes: “r”; read “w”; write “a”; append Ex.: myFile=open(“myFile.txt”, “r”) Reading from files: Three functions: .read()/.read(int) .read() returns a string containing all the characters in the file. .read(int) returns a string containing the first <int> characters in the file. .readline() returns a string containing the next line in the file up to the next newline character. .readlines() returns a list of strings of all the lines in the file. Writing to files: When a file is opened for writing (ie. open(<filename>, “w”)), a new file is created in your current working directory. If that file exists, everything in the file is erased. One function: .write(<“text”>) writes the string to the file To write non-stings to file cast them as strings then write. .write( str( non-string ) )
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 2

FileIO_FunctionalProgramming - CS 1301 File I/O Reference...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online