with open("file.txt") as file_in:
lines = []
for line in file_in:
lines.append(line)
def get_lines(file_name: str) -> [str]:
"""
This function returns the lines from the file as a list.
It handles the opening and closing of the file.
Also, the function assumes the file exists and can be read.
"""
with open(file_name, 'r') as f:
lines = f.readlines()
return lines
# How to use:
lines = get_lines('my_file.txt')
myfile = open("demo.txt", "r")
myline = myfile.readline()
while myline:
print(myline)
myline = myfile.readline()
myfile.close()
f = open("filename")
lines = f.readlines()
# Question: Which function is used to read single line from file?
# Answer: readline()
<?php
$file = fopen("welcome.txt","r")or exit("unable to open file!");
while(!feof($file)){
echo fgets($file)."<br>";
}
fclose($file);
?>