April 2024
Beginner to intermediate
500 pages
24h 20m
English
Files are sequences of characters that are stored on a disk. It is also useful to think of files as sequences of lines. Table B.8 summarizes File methods.
Table B.8 Methods Provided by a File Object
| Name | Use | Explanation |
|---|---|---|
| open (filename, mode) | with open(filename,'r') as fileVar | Opens a file called filename and uses it for reading. This will return a reference (fileVar) to a file object. Other modes are 'w' for writing, and 'a' for appending to a file. Read ('r') is the default. With this use of open, the file is closed automatically when processing finishes. |
| close | fileVar.close() | File use is complete. |
| write | nChars = fileVar.write(aString) | Adds aString to the end of the file. filevar must refer to a file that has been opened ... |
Read now
Unlock full access