python 标准库

作者: Sonia_Du | 来源:发表于2019-03-28 15:26 被阅读0次

    os

    获得当前工作目录

    os.getcwd()

    切换目录

    os.chdir('../123')

    pickle

    保存数据

    pickle.dump(要保存的数据,目标文件)

    import pickle
    try:
        with open('test1.txt','wb') as test1, open('test2.txt','wb') as test2:
            pickle.dump('new world',test1)
            pickle.dump('old world',test2)
    except IOError as ierr:
        print("File Error:"+str(ierr))
    except pickle.PickleError as perr:
        print("Picking Error:"+str(perr))
    

    恢复数据

    pickle.load(目标文件)

    try:
        with open('test1.txt','rb') as test1, open('test2.txt','rb') as test2:
            n_test1 = pickle.load(test1)
    except IOError as ierr:
        print("File Error:"+str(ierr))
    except pickle.PickleError as perr:
        print("Picking Error:"+str(perr))
    
        
    >>> n_test1
    'new world'
    

    相关文章

      网友评论

        本文标题:python 标准库

        本文链接:https://www.haomeiwen.com/subject/runqbqtx.html