From_file=open('D:\\python\\A\\tianqi.txt','a') try: for each_line in From_file: print(each_line) #print(From_file.read()) finally: From_file.close()
使用try和finally防止忘记关闭文件。
使用相对路径,文件夹A和.py文件在一个文件夹内
From_file=open('.\\A\\tianqi.txt') try: for each_line in From_file: print(each_line) finally: From_file.close()