python 学习三

python的文件读取:

filepath='D:\迅雷下载\123.txt'
with open(filepath,encoding='utf-8')as file_object:
    contents=file_object.readlines()

for content in contents:
     print(content.rstrip())

方法:filepath为文件路径,使用关键字with,其open()返回的只是在with代码块内使用。

而,使用readlines()将内容存在列表中,可在with外使用内容。

文件读取的方法比较多,格式也多,

猜你喜欢

转载自www.cnblogs.com/zhjvvvvvv/p/13405793.html