open () function ʻOpen (filename [, mode [, encode [, error handling]]])`
close () function
F.close
read () function
F.read ([integer size])
If an integer size is specified as an option, only the size will be read.
readline () function
F.readline ([integer size])
Reads a line from the file and returns it as a string.
readlines () function
F.readlines ([integer size])
Read multiple lines from a file.
** Read line by line from file and process **
f = open("test.txt", 'r', encoding='utf-8')
for line in f:
print(line, end= " ")
Another option is to use the readlines () method, but this can cause problems when dealing with large files. Unless you have a specific reason, it's best to just add the file object to the for statement.
write () function
F.write (string)
** Open and export the file in the specified mode **
f = open("newfile.txt", "w", encodint="UTF-8")← Open file in mode w
f.write(s)← Write the string of variable s to a file
f.close ← Close the file
Recommended Posts