YouTip LogoYouTip

Python Func Open

# Python2.x Python open() Function [![Image 3: Python Built-in Functions](#) Python Built-in Functions](#) The Python open() function is used to open a file, creating a file object, which allows related methods to be called for reading and writing. For more file operations, refer to: [Python File I/O](#). ### Function Syntax open(name[, mode[, buffering]]) Parameter Description: * name : A string value containing the name of the file you want to access. * mode : mode determines the mode in which the file is opened: read-only, write, append, etc. All possible values are listed in the complete list below. This parameter is optional, and the default file access mode is read-only (r). * buffering : If buffering is set to 0, there will be no buffering. If buffering is set to 1, line buffering will be performed when accessing the file. If buffering is set to an integer greater than 1, it indicates the size of the buffering in bytes. If a negative value is set, the system default buffering size will be used. Complete list of modes for opening files: | Mode | Description | | --- | --- | | t | Text mode (default). | | x | Write mode, creates a new file, and raises an error if the file already exists. | | b | Binary mode. | | + | Opens a file for updating (readable and writable). | | U | Universal newline mode (not recommended). | | r | Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode. | | rb | Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode. Generally used for non-text files like images. | | r+ | Opens a file for both reading and writing. The file pointer is placed at the beginning of the file. | | rb+ | Opens a file for both reading and writing in binary format. The file pointer is placed at the beginning of the file. Generally used for non-text files like images. | | w | Opens a file for writing only. If the file exists, it is opened and truncated from the beginning, meaning the existing content is deleted. If the file does not exist, a new file is created. | | wb | Opens a file for writing only in binary format. If the file exists, it is opened and truncated from the beginning, meaning the existing content is deleted. If the file does not exist, a new file is created. Generally used for non-text files like images. | | w+ | Opens a file for both reading and writing. If the file exists, it is opened and truncated from the beginning, meaning the existing content is deleted. If the file does not exist, a new file is created. | | wb+ | Opens a file for both reading and writing in binary format. If the file exists, it is opened and truncated from the beginning, meaning the existing content is deleted. If the file does not exist, a new file is created. Generally used for non-text files like images. | | a | Opens a file for appending. If the file exists, the file pointer is placed at the end of the file. This means new content will be written after the existing content. If the file does not exist, a new file is created for writing. | | ab | Opens a file for appending in binary format. If the file exists, the file pointer is placed at the end of the file. This means new content will be written after the existing content. If the file does not exist, a new file is created for writing. | | a+ | Opens a file for both reading and appending. If the file exists, the file pointer is placed at the end of the file. The file will be opened in append mode. If the file does not exist, a new file is created for reading and writing. | | ab+ | Opens a file for both reading and appending in binary format. If the file exists, the file pointer is placed at the end of the file. If the file does not exist, a new file is created for reading and writing. | ### file Object Methods * **file.read()**: If size is not specified, it returns the entire file. If the file size is >2 times the memory, there might be issues. f.read() returns "" (empty string) when it reaches the end of the file. * **file.readline()**: Returns one line. * **file.readlines()**: Returns a list containing `size` lines. If size is not specified, it returns all lines. * **for line in f: print line**: Accesses via an iterator. * **f.write("hellon")**: If you want to write data other than strings, convert it to a string first. * **f.tell()**: Returns an integer indicating the current position of the file pointer (i.e., the number of bytes from the beginning of the file). * **f.seek(offset[, whence])**: Used to move the file pointer. * offset: The unit is bytes, can be positive or negative. * whence: 0 - Beginning of file (default); 1 - Current position; 2 - End of file. * **f.close()** Closes the file. > For more content, refer to: (#) ### Example Test file test.txt, with the following content: TUTORIAL1 TUTORIAL2 >>>f = open('test.txt')>>>f.read()'TUTORIAL1n TUTORIAL2n' [![Image 4: Python Built-in Functions](#) Python Built-in Functions](#)
← Python Func EnumeratePython Built In Functions β†’