学习笔记(17):21天通关Python(仅视频课)-案例实操:按行读取文件

立即学习:https://edu.csdn.net/course/play/24797/282197?utm_source=blogtoedu

# readLine while 循环读取所有行
# try:
#     f = open('test44.py', 'r', True, 'utf-8')
#     while True:
#         data = f.readline()
#         if not data:
#             break
#         print(data, end='')
# except OSError as e:
#     print(e)
#     print(e.errno)
#     print(e.args)
#     print(e.strerror)
# finally:
#     if 'f' in globals():
#         f.close()

# readLines 循环读取所有行
# try:
#     f = open('test44.py', 'r', True, 'utf-8')
#     #返回的是一个列表
#     data = f.readlines()
#     for i in data:
#         print(i,end='')
#     print(data, end='')
# except OSError as e:
#     print(e)
#     print(e.errno)
#     print(e.args)
#     print(e.strerror)
# finally:
#     if 'f' in globals():
#         f.close()

# for 方法 读取所有数据
# try:
#     f = open('test44.py', 'r', True, 'utf-8')
#     for i in f:
#         print(i,end='')
# except OSError as e:
#     print(e)
#     print(e.errno)
#     print(e.args)
#     print(e.strerror)
# finally:
#     if 'f' in globals():
#         f.close()

# lincache.getline
# import linecache
# try:
#     #data = linecache.getline('test44.py',10)
#     data = linecache.getlines('test44.py')
#     print(data)
#     for i in data:
#         print(i,end='')
# except:
#     print('出现问题了')

# lincache.getline
import linecache

try:
    # data = linecache.getline('test44.py',10)
    data = linecache.getlines(linecache.__file__)
    print(data)
    for i in data:
        print(i, end='')
except:
    print('出现问题了')
发布了25 篇原创文章 · 获赞 4 · 访问量 599

猜你喜欢

转载自blog.csdn.net/happyk213/article/details/105204788