Answers for "what is read() in python"

2

read function in python

Syntax: file.read(fd, n)

Parameter:
fd: A file descriptor representing the file to be read.
n: Optional. An integer value denoting the number of bytes to be read from the file associated with the given file descriptor fd.
   The number of bytes to return. Default -1, which means the whole file.

Return Type: This method returns a bytestring which represents the bytes read from the file associated with the file descriptor fd.
Posted by: Guest on May-27-2021
4

python write to file

# open a file you can use the function open
file = open("myFile.txt", "w")
#here we have "open", and that's the main function
# that opens the file, next we have 2 arguments
# FILENAME and OPENING MODE
# in the filename argument you just have to write the file's location
# or if the script is in that location just write the filename
# in the opening mode you have to write in which mode you want
# to open your file, i'll list some here:
# "w" for writing to a file
# "r" for reading to a file
# "r+" for both reading and writing
# "a" to append to a file
#to write to the file use "file.write"
file.write("This has been written by a program")
#and finally to close the file when you're done with it
file.close()
# hope this helped and remember that in "w" mode it
#deletes the content of the file and replaces
# it with a new one, if you want to add something
# to a file use "a" mode
Posted by: Guest on November-18-2020

Python Answers by Framework

Browse Popular Code Answers by Language