2021-10-22

Python Day3 

今天主要学习的是python中的类、模块等概念和使用还有最重要的读写!

换了个编译器vscode但是感觉没有之前sublime text好用啊!特别终端输出一大串没用的信息用关闭掉

只能先用外部终端了TAT

with open('programing.txt','w') as file_object:#open(,)第二个参数是确定打开方式包括:写入'w'/读取'r'/附加模式'a'/读写模式'r+'/默认读取

    file_object.write("I love programming.")

file_name='programing.txt'

with open(file_name) as file_object:

    message=file_object.read()

print(message)

------------------------------------------------------------------------分割线--------------------------------------------------------------------

file_name='programing.txt'

#open(,)第二个参数是确定打开方式包括:写入'w'/读取'r'/附加模式'a'/读写模式'r+'/默认读取

#w 写入覆盖原同名文件,没有即新建

with open(file_name,'w') as file_object:

    file_object.write("I love programming.\n")

    file_object.write("I love creating new games.\n")

#附加方式写入不会覆盖原文件,仅在末尾增加新内容。

with open(file_name,'a') as file_object:

    file_object.write("I also love finding meanings in a large datasets.\n")

    file_object.write("I love creating apps that can run in a brower\n")

try:

    with open(file_name,encoding='utf-8') as file_object:

        message=file_object.read()

except FileNotFoundError:

    print(f"Sorry,the file {file_name} does not exist.")

else:

    print(message.rstrip())

你可能感兴趣的:(2021-10-22)