Files
File objects are handled with built-in functions. The open method is used to open a file for reading or writing operations.
The following table shows the most common methods used for files
F
Operation | Description |
f = open(filename [, mode='r']) | Opens a file in the proper mode: mode : 'r' : Read 'w' : Write. Create the file if it does not exist 'a' : Append. '+' : (added to the previous modes - example 'a+') opens the file for updates 'b' : (added to the previous modes - example 'rb') open the file in binary mode |
f.close() | Closes the f file |
f.fileno() | Returns descriptor of the f file |
f.flush() | Empties the internal buffer of f |
f.isatty() | Returns true if f is a TTY |
f.read([size]) | Reads a maximum of size bytes from the file and returns a string |
f.readline() | Reads a line from f |
f.readlines() | Reads until the end of file (EOF) and returns a list of lines |
f.xreadlines() | Returns a sequence without reading until the end of file (preferred to readlines() |
f.seek(offset[, whence=0]) | Sets the file's current position to 'offset' bytes from 'whence': 0: from the beginning of the file 1: from the current location 2: from the end of the file |
f.tell() | Returns the current location in the file |
f.write(str) | Writes str into the file |
f.writelines(list) | Writes a list of strings in the file |
No comments:
Post a Comment