Python io流

input output stream,主要是指的计算机输入输出的操作,一般来说是内存与磁盘之间的输入和输出(侠义)

io流操作是一种持久化操作,是将数据持久化在磁盘上

Python如何操作io流?

通过全局函数open--------主要作用是打开本地的文件

open函数解析:

        可以在cmd界面,先用py进入py终端,help(open)


C:\Users\86156>py
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> help(open)
Help on built-in function open in module io:

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
    Open file and return a stream.  Raise OSError upon failure.

    file is either a text or byte string giving the name (and the path
    if the file isn't in the current working directory) of the file to
    be opened or an integer file descriptor of the file to be
    wrapped. (If a file descriptor is given, it is closed when the
    returned I/O object is closed, unless closefd is set to False.)

    mode is an optional string that specifies the mode in which the file
    is opened. It defaults to 'r' which means open for reading in text
    mode.  Other common values are 'w' for writing (truncating the file if
    it already exists), 'x' for creating and writing to a new file, and
    'a' for appending (which on some Unix systems, means that all writes
    append to the end of the file regardless of the current seek position).
    In text mode, if encoding is not specified the encoding used is platform

    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
    'b'       binary mode
    't'       text mode (default)
    'U'       universal newline mode (deprecated)

-- More  --

 可以看到open这个全局函数的功能很强大

做个实例:咱们在安装Python的文件夹下面新建一个文本文档,命名为a.txt,在里面随便输入几个字

 现在咱去把他读出来

D:\应用程序\Python\代码>py
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from os import path
>>> path.abspath(".")#获取你当前文件所在的路径
'D:\\应用程序\\Python\\代码'
>>> open("D:\\应用程序\\Python\\代码\\a.txt")#打开这个文件
<_io.TextIOWrapper name='D:\\应用程序\\Python\\代码\\a.txt' mode='r' encoding='cp936'>
>>> f=open("a.txt","r")#将所打开的a.txt文件内容读出来,赋值给f
>>> f.read()#读出f的内容
'哈哈哈hhhh'
>>>

既然读出来了,那咱们写进去呢

>>> f=open("a.txt",mode="w")
>>> f.write("you are beautiful!!")
19
>>> f.close()#注意,这里一定要闭流,否则对文件的操作有影响
>>>

咱去把写过的文件读出来

有没有发现,我之前的内容被覆盖掉了,这个内容是我后面写的,之前写的直接被他覆盖。

>>> f=open("a.txt",mode="w")
>>> f.write("you are beautiful!!")
19
>>> f.close()
>>> f=open("a.txt","r")
>>> f.read()
'you are beautiful!!'
>>>

 所以  咱们还有另外一种方式,那就是可以边写边读边读边写那种。

现在我a.txt文件里面是只有you are  beautiful!!这些数据了吧  

咱们直接在他后面加一些数据,让它实现为:‘you are a beautiful!!----thanks!!’

>>> f=open("a.txt","a")
>>> f=open("a.txt",mode="a")#只需要将mode参数改为a就可以了
>>> f.write("----thanks!!")
12
>>>
>>> f=open("a.txt","r")
>>> f.read()
'you are beautiful!!----thanks!!'
>>>

ok,这样就好了 ,注意的是在对文件操作结束,一定要进行闭流,否则他会出现一种错误情况,例如:你打开一个文件,然后你直接删除他,他就会直接报错。

 类似于这种情况。

好的,这就是本期讲的io流一小部分,咱们下期在说!再见

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值