V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
lostapple
V2EX  ›  Python

python file.truncate() 然后 file.write() file.read() 出现乱码

  •  
  •   lostapple · 2016-07-28 18:58:58 +08:00 · 2385 次点击
    这是一个创建于 2827 天前的主题,其中的信息可能已经有所发展或是发生改变。

    ##dirs0.txt 内容:

    1234567890123456789012345678901234567890123456789012345678901234567890
    

    ##代码文件: testfile.py

    def test(flag=None):
        with open('dirs0.txt', 'r+') as f:
            print f.read(8)
            f.truncate()
            f.write('+')
            f.write('*')
            if flag:
                f.read()
    
    test() 
    

    ##测试

    1. test() 执行结果正常, dirs0.txt 内容变为: 12345678+*

    2. test(True) 执行结果异常, dirs0.txt 内容有乱码: 12345678+34567890123456789012345678901234567890123456789012345678901234567890elib.rpc unpickle_code p4 (S'c\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00@\x00\x00\x00s\x1a\x00\x00\x00d\x02\x00d\x00\x00\x84\x01\x00Z\x01\x00e\x01\x00d\x01\x00\x83\x01\x00\x01d\x02\x00S(\x03\x00\x00\x00c\x01\x00\x00\x00\x02\x00\x00\x00\x06\x00\x00\x00C\x00\x00\x00sa\x00\x00\x00t\x00\x00d\x01\x00d\x02\x00\x83\x02\x00\x8fL\x00}\x01\x00|\x01\x00j\x01\x00d\x03\x00\x83\x01\x00GH|\x01\x00j\x02\x00\x83\x00\x00\x01|\x01\x00j\x03\x00d\x04\x00\x83\x01\x00\x01|\x01\x00j\x03\x00d\x05\x00\x83\x01\x00\x01|\x00\x00rW\x00|\x01\x00j\x01\x00\x83\x00\x00\x01n\x00\x00Wd\x00\x00QXd\x00\x00S(\x06\x00\x00\x00Ns\t\x00\x00\x00dirs0.txts\x02\x00\x00\x00r+i\x08\x00\x00\x00t\x01\x00\x00\x00+t\x01\x00\x00\x00(\x04\x00\x00\x00t\x04\x00\x00\x00opent\x04\x00\x00\x00readt\x08\x00\x00\x00truncatet\x05\x00\x00\x00write(\x02\x00\x00\x00t\x04\x00\x00\x00flagt\x01\x00\x00\x00f(\x00\x00\x00\x00(\x00\x00\x00\x00s!\x00\x00\x00C:\Users\texs\Desktop\testfile.pyt\x04\x00\x00\x00test\x02\x00\x00\x00s\x0e\x00\x00\x00\x00\x01\x12\x01\x0e\x01\n\x01\r\x01\r\x01\x06\x01i\x01\x00\x00\x00N(\x02\x00\x00\x00t\x04\x00\x00\x00NoneR\x08\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00s!\x00\x00\x00C:\Users\texs\Desktop\testfile.pyt\x08\x00\x00\x00<module>\x02\x00\x00\x00s\x02\x00\x00\x00\x0c\t' tRp5 tp6 (dp7 ttp8 tp9 . S R& 垶_ 訸 ?  c @ s d d ? Z e d ? d S( c C sa t d d ? 廘 } | j d ? GH| j ? | j d ? | j d ? | rW | j ? n Wd QXd S( Ns dirs0.txts r+i t +t *( t opent readt truncatet write( t flagt f( ( s! C:\Users\texs\Desktop\testfile.pyt test s

    i N( t NoneR ( ( ( s! C:\Users\texs\Desktop\testfile.pyt <module> s ... ...


    ##说明: **f.truncate 之后, f.write() 和 f.read() 同时使用才会有乱码,单独使用 write() 或 read() 没有乱码

    发现 f.write('+') f.write('*') 占用了两个字,然后追加到 dirs0.txt 文件尾部的内容是原始文件内容除去头部两个字的部分 +*34567890123456789012345678901234567890123456789012345678901234567890 1234567890123456789012345678901234567890123456789012345678901234567890

    进一步有了删除文件中指定行代码(会在文件后添加乱码):

    
    def test(flag=None, lineno=1):
        with open('dirs0.txt', 'r+') as f:
            for n in range(lineno-1):
                f.readline()
            tempno1 = f.tell()
            f.readline()
            tempno2 = f.tell()
            f.truncate(tempno1)
            f.write(' '*(tempno2-lineno-1)+'\n')
            
            if flag:
                f.read()
    
    test(1, lineno=3)
    

    最后 问题:

    -1 ) f.read() 为啥会有内容写入文件,而且 f.write() 和 f.read() 要一起用才会有写入?

    -2) 为什么会出现乱码?

    多谢大侠指教^_^

    1 条回复    2016-07-28 20:15:34 +08:00
    perpyy
        1
    perpyy  
       2016-07-28 20:15:34 +08:00
    写了后, 先 seek(0) 或者 flush , 再 read
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4030 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 00:58 · PVG 08:58 · LAX 17:58 · JFK 20:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.