> f.read() # Read entire file into a string > f.close() # Open the file for reading and read the entire file via read() > f.readline() # Return an empty string after EOF > f.readlines() # Read all (next) lines into a list of strings > f.readline() # Read next line into a string > f.close() # Always close the file # Check the contents of the file created # Open the file created for reading and read line(s) using readline() and readlines() The '\n' will be translated to the platform-dependent newline ( '\r\n' for Windows or '\n' for Unixes/Mac OS).Įxamples # Open a file for writing and insert some records You need to explicitly terminate the str with a '\n', if needed. fileObj.write( str) -> int: Write the given string to the file and return the number of characters written.fileObj.read() -> str: Read the entire file into a string.fileObj.readlines() -> : Read all lines into a list of strings.It returns an empty string after the end-of-file (EOF). fileObj.readline() -> str: (most commonly-used) Read next line (upto and include newline) and return a string (including newline).It initially positions at the beginning of the file and advances whenever read/write operations are performed. The fileObj returned after the file is opened maintains a file pointer. fileObj.close(): Flush and close the file stream.You can optionally specify the text encoding via keyword parameter encoding, e.g., encoding="utf-8". You can also use 'rb', 'wb', 'ab', 'rb+' for binary mode (raw-byte) operations. The available modes are: 'r' (read-only - default), 'w' (write - erase all contents for existing file), 'a' (append), 'r+' (read and write). open( file, ) -> fileObj: Open the file and return a file object.Python provides built-in functions and modules to support these operations. Open the file for read or write or both.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |