python之再学习----简单的文件

# filename:python3.1.py
# author:super
# date:2018-03-01


# with open(r'd:\test\test.txt') as file_object:
# content = file_object.read()
# print(content.rstrip())
#
# filename = r'd:\test\test.txt'
# with open(filename) as file:
# for i in file:
# print(i.rstrip())

# 用open方法就不用close()了 , open里面会自带的
# 用open的时候那会又不想用这个文件, 可以用file.readlines() 存储起来
# 再用for来把readlines()里面的内容读出来
filename = r'd:\test\test.txt'
with open(filename) as files:
lines = files.readlines()
pi_string = ''
# 在读取文件内容的时候,读出来的内容均为字符串, 如果要作为int来使用, 则要int()来转换
for line in lines:
pi_string += line.strip()
print(line.rstrip())
print(pi_string)
posted @ 2018-03-01 09:25  super-JAVA  阅读(119)  评论(0编辑  收藏  举报