filename = 'pi_digits.txt'

with open(filename) as f:#默认以只读方式打开文件
    lines = f.readlines()#读取所有行,结果为列表,每行为列表一元素

for line in lines:
    print(line.rstrip())
    
with open(filename) as f:#默认以只读方式打开文件
    lines = f.read()#读取所有内容给变量,结果是一串字符
print(lines)

for line in lines:#将一字符串以单个字符分别显示
    #print(line.rstrip())
    pass
filename = 'programming.txt'

with open(filename, 'a') as file_object:#以追加方式写入文件,在原有内容后面增加新内容
    file_object.write("I also love finding meaning in large datasets.\n")
    file_object.write("I love creating apps that can run in a browser.\n")


filename = 'programming.txt'

with open(filename, 'w') as file_object:#以新建方式写入文件,输入新内容
    file_object.write("I also love finding meaning in large datasetsaaa.\n")
    file_object.write("I love creating apps that can run in a browseraaa.\n")

 

相关文章:

  • 2022-01-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
  • 2021-12-21
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-01
  • 2022-02-24
  • 2021-08-02
  • 2021-11-23
相关资源
相似解决方案