# Open file fileHandler = open ("data.txt", "r") # Get list of all lines in file listOfLines = fileHandler.readlines() # Close file fileHandler.close()
它将返回文件中的行列表。我们可以遍历该列表,并剥离()新行字符,然后打印该行,即
1 2 3
# Iterate over the lines for line in listOfLines: print(line.strip())
输出:
1 2 3 4 5 6
Hello This is a sample file that contains is some text is like 123
fileHandler = open ("data.txt", "r") while True: # Get next line from file line = fileHandler.readline() # If line is empty then end of file reached if not line : break; print(line.strip()) # Close Close fileHandler.close()
输出:
1 2 3 4 5 6
Hello This is a sample file that contains is some text is like 123
Hello This is a sample file that contains is some text is like 123
在这种情况下,当控制脱离块时,文件将自动关闭。即使由于某些异常而出现阻塞。
使用上下文管理器(带块)获取文件中的行列表
让我们遍历文件中的所有行并创建行列表,即
1 2 3 4 5 6
# Get the all the lines in file in a list
listOfLines = list() with open ("data.txt", "r") as myfile: for line in myfile: listOfLines.append(line.strip())
listOfLines列表的内容为
1 2
['Hello', 'This is a sample', 'file that contains is', 'some text', 'is', '', 'like 123']
使用上下文管理器和while循环逐行读取文件的内容
让我们使用上下文管理器和while循环遍历文件中的各行,即
1 2 3 4 5 6 7 8 9
# Open file
with open("data.txt", "r") as fileHandler: # Read next line line = fileHandler.readline() # check line is not empty while line: print(line.strip()) line = fileHandler.readline()
列表的内容将是
1 2 3 4 5 6 7
Hello This is a sample file that contains is some text is like 123
def main(): print("****Read all lines in file using readlines() *****") # Open file fileHandler = open("data.txt", "r") # Get list of all lines in file listOfLines = fileHandler.readlines() # Close file fileHandler.close() # Iterate over the lines for line in listOfLines: print(line.strip()) print("****Read file line by line and then close it manualy *****") # Open file fileHandler = open("data.txt", "r") while True: # Get next line from file line = fileHandler.readline() # If line is empty then end of file reached if not line: break; print(line.strip()) # Close Close fileHandler.close() print("****Read file line by line using with open() *****") # Open file with open("data.txt", "r") as fileHandler: # Read each line in loop for line in fileHandler: # As each line (except last one) will contain new line character, so strip that print(line.strip()) print("****Read file line by line using with open *****") # Get the all the lines in file in a list listOfLines = list() with open("data.txt", "r") as myfile: for line in myfile: listOfLines.append(line.strip()) print(listOfLines) print("****Read file line by line using with open() and while loop *****") # Open file with open("data.txt", "r") as fileHandler: # Read next line line = fileHandler.readline() # check line is not empty while line: print(line.strip()) line = fileHandler.readline()
****Read all lines in file using readlines() ***** Hello This is a sample file that contains is some text is like 123 ****Read file line by line and then close it manualy ***** Hello This is a sample file that contains is some text is like 123 ****Read file line by line using with open() ***** Hello This is a sample file that contains is some text is like 123 ****Read file line by line using with open ***** ['Hello', 'This is a sample', 'file that contains is', 'some text', 'is', '', 'like 123'] ****Read file line by line using with open() and while loop ***** Hello This is a sample file that contains is some text is like 123