2.27

# readlines()  read()

with open('pi_digits.txt') as file_object: #函数open()接受一个参数:要打开的文件的名称。
    contents = file_object.read()
    print(contents)
    print(contents.rstrip())


filename = 'pi_digits.txt'  #
with open(filename) as file_object: #
    for line in file_object: #
        print(line.rstrip())
"""在❶处,我们将要读取的文件的名称存储在变量filename 中,这是使用文件时一种常见的做法。由于变量filename 表示的并非实际文件——它只是一个让Python知道到哪里
去查找文件的字符串,因此可轻松地将'pi_digits.txt' 替换为你要使用的另一个文件的名称。调用open() 后,将一个表示文件及其内容的对象存储到了变
量file_object 中(见❷)。这里也使用了关键字with ,让Python负责妥善地打开和关闭文件。为查看文件的内容,我们通过对文件对象执行循环来遍历文件中的每一行(见
❸)。"""


filename = 'pi_digits.txt' # 将要读取的文件的名称存储在变量filename 中,这是使用文件时一种常见的做法
with open(filename) as file_object:
    lines = file_object.readlines() #
for line in lines: #
    print(line.rstrip())

"""❶处的方法readlines() 从文件中读取每一行,并将其存储在一个列表中;接下来,该列表被存储到变量lines 中;在with 代码块外,我们依然可以使用这个变量。在❷
处,我们使用一个简单的for 循环来打印lines 中的各行。由于列表lines 的每个元素都对应于文件中的一行,因此输出与文件内容完全一致。"""

filename = 'pi_digits.txt'
with open(filename) as file_object:
    lines = file_object.readlines()

pi_string = '' #
for line in lines: #
    pi_string += line.rstrip()

print(pi_string) #
print(len(pi_string))

"""就像前一个示例一样,我们首先打开文件,并将其中的所有行都存储在一个列表中。在❶处,我们创建了一个变量——pi_string ,用于存储圆周率的值。接下来,我们使用
一个循环将各行都加入pi_string ,并删除每行末尾的换行符(见❷)。在❸处,我们打印这个字符串及其长度:"""

filename = 'pi_30_digits.txt'  # 问题 打开文件名不同
with open(filename) as file_object:
    lines = file_object.readlines()

pi_string = ''
for line in lines:
    pi_string += line.strip()

print(pi_string)
print(len(pi_string))

"""
10.1.7 圆周率值中包含你的生日吗
我一直想知道自己的生日是否包含在圆周率值中。下面来扩展刚才编写的程序,以确定某个人的生日是否包含在圆周率值的前1 000 000位中。为此,可将生日表示为一个由数字
组成的字符串,再检查这个字符串是否包含在pi_string 中:
filename = 'pi_million_digits.txt'
with open(filename) as file_object:
lines = file_object.readlines()
pi_string = ''
for line in lines:
pi_string += line.rstrip()
❶ birthday = input("Enter your birthday, in the form mmddyy: ")
❷ if birthday in pi_string:
print("Your birthday appears in the first million digits of pi!")
else:
print("Your birthday does not appear in the first million digits of pi.")
在处,我们提示用户输入其生日,在接下来的❷处,我们检查这个字符串是否包含在pi_string 中。运行一下这个程序:"""

file_name = 'learning_python.txt'
with open(file_name) as file_object:
    lines = file_object.read()

    #lines = file_object.readline()
    leanring_python = ''
    for line in lines: #
        leanring_python += line.rstrip()

print(leanring_python)

filename = 'programming.txt'
with open(filename, 'w') as file_object: #
    file_object.write("I love programming.") #
"""在这个示例中,调用open() 时提供了两个实参(见❶)。第一个实参也是要打开的文件的名称;第二个实参('w' )告诉Python,我们要以写入模式 打开这个文件。打开文件
    时,可指定读取模式 ('r' )、写入模式 ('w' )、附加模式 ('a' )或让你能够读取和写入文件的模式('r+' )。如果你省略了模式实参,Python将以默认的只读模式打
    开文件。
    如果你要写入的文件不存在,函数open() 将自动创建它。然而,以写入('w' )模式打开文件时千万要小心,因为如果指定的文件已经存在,Python将在返回文件对象前清空
    该文件。
    在❷处,我们使用文件对象的方法write() 将一个字符串写入文件。"""
Python只能将字符串写入文本文件。要将数值数据存储到文本文件中,必须先使用函数str() 将其转换为字符串格式。

filename = 'programming.txt'
with open(filename, 'w') as file_object:
    file_object.write("I love programming.")
    file_object.write("I love creating new games.")
"""
filename = 'programming.txt'
with open(filename, 'a') as file_object:
    file_object.write("I also love finding meaning in large datasets.
")
    file_object.write("I love creating apps that can run in a browser.
")
"""

filename = 'guest.txt'
while True:
    message = "Please tell me your name.
" + "Enter 'q' to quit."
    user_name = input(message)
    if user_name == 'q': # 此处形参不可以用input(message) 否则会让用户输入两次
        break
    else:
        print("Hello, " + user_name.title() + "!")
        with open(filename,'a') as file_object:
            file_object.write(str(user_name) + " is login.
")
View Code
原文地址:https://www.cnblogs.com/phyllissRyo/p/10447797.html