博客
关于我
读写文件 及 输入和输出 知识详解
阅读量:556 次
发布时间:2019-03-09

本文共 3078 字,大约阅读时间需要 10 分钟。

目录

读写文件

使用的工具:

open(file,     #代表打开(读或者写)哪个文件,​                       绝对路径/相对路径  ​          mode='r',  #代表打开文件的方式​                             读/写的方式,文本/二进制​          buffering=-1,  #缓存​          encoding=None,  #编码:UTF-8,GBK​          errors=None,   #错误处理:strict​          newline=None,  #新起一行​          closefd=True,  #关闭文件描述符(Linux用的较多)
========= ===============================================================    Character Meaning--------- ---------------------------------------------------------------​    'r'       open for reading (default)​              打开方式默认为读​    'w'       open for writing, truncating the file first​              以写的方式打开,先删除文件,然后建立空文件​    'x'       create a new file and open it for writing​              创建新文件并以写的方式打开​    'a'       open for writing, appending to the end of the file if it exists​              以写的方式打开,如果文件存在将追加到文件的末尾​    'b'       binary mode​              二进制模式​    't'       text mode (default)​              默认为文本模式​    '+'       open a disk file for updating (reading and writing)​              打开磁盘文件并更新(读写)​  ========= ===============================================================

读写文件练习:

读写一个文档:

file_obj = open("review", mode="rt", encoding="utf-8")data = file_obj.read()   #读review这个文件file_obj_w = open("tushuguan.txt", mode="wt", encoding="utf-8")file_obj_w.write(data)   #创建 tushuguan.txt 文件,并将 revew 文件内容写进该文件file_obj.close()file_obj_w.close()

运行上边程序会新建一个“tushuguan.txt”文件,并将"review"文件内容复制到“tushuguan.txt”文件中。

读写一张照片:

file_obj = open("python.jpg", mode="rb")data = file_obj.read()file_obj_w = open("python_cp.jpg", mode="wb")file_obj_w.write(data)file_obj.close()file_obj_w.close()

运行完成后会将照片"python.jpg"复制到新创建的"python_cp.jpg"文件中

读写文件的其他应用:

with open("huyi.txt", mode="a", encoding="utf-8") as file_obj:  #将内容追加到文件末尾    file_obj.write("我学习关于云计算网络安全的知识")  #追加一行内容    file_obj.writelines(["\n目前正在学习python语言", "\n已经学习了hcia和rhcsa课程"])   #追加多行内容with open("review", encoding="utf-8") as file_obj:    # data = file_obj.read(10)    #指定读取字符的个数    data = file_obj.readlines(50)    #读取到满足字符的所有行的内容    print(data)

输入和输出

输入:

data = input("请输入:")print(data, type(data))    #执行后输入任何内容,数据类型总为str

格式化输出:

name = "小明"age = 20#格式:My name is xxx, My age is 20print("My name is ", name, "My age is ", age)print("My name is %s, My age is %d" % (name, age))print(f"My name is {name}, My age is {age}")#format函数print("My name is {}, My age is {}".format(name, age))print("My name is {0}, My age is {1}".format(name, age))print("My name is {name}, My age is {age}".format(name=name, age=age))

输出结果为:

在这里插入图片描述

打印表格练习:

print("{0:^5}{1:^15}{2:^15}".format("姓名", "联系方式", "学校"))print("{0:*<5}{1:*<15}{2:*<15}".format("张三", "11111111111", "西安石油大学"))print("{0:|^5}{1:|^15}{2:|^15}".format("里斯", "22222222222", "西安交通工程学院"))print("{0:->5}{1:->15}{2:->15}".format("王五", "33333333333", "西安国防学院"))

另一种写法:

print(f"{'姓名':^5}{'联系方式':^15}{'学校':^30}")print(F"{'张三':*<5}{'11111111111':*<15}{'西安石油大学':*<15}")print(f"{'里斯':*<5}{'22222222222':*<15}{'西安交通大学':*<15}")print(F"{'王五':*<5}{'33333333333':*<15}{'西安科技大学':*<15}")

符号说明:

<:左对齐
^:居中对齐
>:右对齐
*:填充空白字符(也可以填充其他符号)

输出结果为:

在这里插入图片描述

转载地址:http://ockpz.baihongyu.com/

你可能感兴趣的文章
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>
MySQL中interactive_timeout和wait_timeout的区别
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>
mysql中kill掉所有锁表的进程
查看>>
mysql中like % %模糊查询
查看>>
MySql中mvcc学习记录
查看>>
mysql中null和空字符串的区别与问题!
查看>>