zoukankan      html  css  js  c++  java
  • seek()异常

    f=open(‘aabb.py’,‘w’)
    f.write(b’0123456789abcdef’)
    Traceback (most recent call last):
    File “<pyshell#1>”, line 1, in
    f.write(b’0123456789abcdef’)
    TypeError: write() argument must be str, not bytes

    x=f.write(b’0123456789abcdef’)
    Traceback (most recent call last):
    File “<pyshell#2>”, line 1, in
    x=f.write(b’0123456789abcdef’)
    TypeError: write() argument must be str, not bytes

    x=(b’0123456789abcdef’)
    s=str(x)
    f.write(s)
    19

    f.seek(5)
    5

    f.seek(1)
    1

    f.seek(-3,2) #此处出现错误是因为在文本文件中,没有使用b模式选项打开的文件,只允许从文件头开始计算相对位置,从文件尾计算时就会引发异常。
    Traceback (most recent call last):
    File “<pyshell#8>”, line 1, in
    f.seek(-3,2)
    io.UnsupportedOperation: can’t do nonzero end-relative seeks
    以上出现的全部错误改正得

    f=open(‘aabb.py’,‘rb+’) #以二进制格式打开一个文件用于读写
    f.write(b’0123456789abcdef’)
    16

    f.seek(-3,2)
    16

  • 相关阅读:
    POJ 3093 Margaritas on the River Walk(背包)
    BZOJ 2287 【POJ Challenge】消失之物(DP+容斥)
    WC2017 Day1
    WC2017 Day0
    WC2017 Conclusion
    WC2017 Day6
    UOJ #58 糖果公园
    WC2017 Day5
    codevs 1946 阿狸的打字机
    HDU 2457 DNA_repair
  • 原文地址:https://www.cnblogs.com/llb123/p/13398757.html
Copyright © 2011-2022 走看看