YouTip LogoYouTip

Python3 File Read

# Python3.x Python3 File read() Method [![Image 3: Python3 File Methods](#) Python3 File Methods](#) * * * ### Overview The **read()** method is used to read a specified number of characters (in text mode `t`) or bytes (in binary mode `b`) from a file. If the parameter **size** is not given or is negative, it reads the entire content of the file. ### Syntax The syntax for the read() method is as follows: fileObject.read(); ### Parameters * **size** -- The number of characters (in text mode) or bytes (in binary mode) to read from the file. The default is **-1**, which means the entire file is read. ### Return Value Returns the bytes read from the string. ### Example The following example demonstrates the use of the read() method: The content of the file `.txt` is as follows: This is line 1, this is line 2, this is line 3, this is line 4, this is line 5. Looping to read the file content: ## Example #!/usr/bin/python3 # Open the file fo =open(".txt","r+") print("File name: ", fo.name) line = fo.read(10) print("Read string: %s" % (line)) # Close the file fo.close() The output of the above example is: File name: .txt Read string: This is the first line this is the second * * Python3 File Methods](#)
← Java String IsemptyJava Arraylist Removeif β†’