python exercise file

版权声明:本文章为博主原创文章,未经博主允许不得转载,如有问题,欢迎留言交流指正 https://blog.csdn.net/finalkof1983/article/details/88766160

open函数一些说明
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

mode可选参数列表   
     '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互斥,当seek操作时,当偏移位置设置为1或2时,需要以该模式打开文件,另外pickle模块dump操作也需要二进制模式。
    't'        text mode (default),文本方式打开文件
    '+'       open a disk file for updating (reading and writing),与r、w、a结合使用。注意r+、w+、a+都表示读写文件,主要区别在于指针的起始位置,其中r+指针在文件开头,此时写会覆盖文件内容。w+清空文件,指针放在文件开头。a+指针在文件结尾,如果要读取文件需要seek移动。

#定义函数,使用with方式打开文件,写入abc,移动指针到文件头,读取两个字符并返回
def testfileops(filename):
    with open(filename,"w+") as testfile:
        testfile.write("abc")
        position_seek_before = testfile.tell()
        testfile.seek(0)
        position_seek_after = testfile.tell()
        context = testfile.read(2)
    return position_seek_before,context,position_seek_after


print(testfileops(r'f:\pythonfiletest\testfile'))
#定义函数,二进制打开文件,二进制写入123,获得文件描述符,当前位置向前一位移动指针,读取目前位置到文件结尾的所有字符,关闭文件并返回
#seek(offset,whence=0)(whence为0从文件头开始计算偏移,为1从当前位置计算偏移,为2从文件尾计算偏移)
def testfileops(filename):
    testfile = open(filename,'wb+')
    testfile.write(b"123")
    file_descriptor = testfile.fileno()
    testfile.seek(-1,1)
    context = testfile.read()
    testfile.close()
    return  file_descriptor,context


print(testfileops(r'f:\pythonfiletest\testfile'))
#定义函数,使用readline以及readlines读出文件内容,其中readline会读出文件换行符,用end将其屏蔽掉
def testfileops(filename):
    with open(filename,"r") as testfile:
        contextline_part = testfile.readline(2)
        testfile.seek(0)
        contextline = testfile.readline()
        testfile.seek(0)
        contextlines = testfile.readlines()
    print(contextline_part)
    print(contextline,end="")
    print(contextlines)


testfileops(r'f:\pythonfiletest\testfile')

#输出
12
123
['123\n', 'abc\n', '456\n', 'xyz\n']
# 按行遍历文件,打印奇数行
def testfileops(filename):
    with open(filename, "r") as testfile:
        for i,j in enumerate(testfile):
            if i % 2 == 0:
                print(j,end="")
        testfile.seek(0)
        for k in testfile.readlines():
            print(k, end="")


testfileops(r'f:\pythonfiletest\testfile')

pickle模块的一些说明

pickle模块主要用于将程序中运行的对象信息保存到文件中去,永久存储(dump方法),以及反序列化操作从文件中创建上一次程序保存的对象(load方法)

import pickle
def testfileops(filename):
    alist = [1,2,3,4,5]
    atuple = ('a', 'b', 'c', 'd', 'e')
    adict = {1:'a', 2:'b', 3:'c', 4:'d',5:'e'}
    with open(filename,"wb+") as testfile:
        pickle.dump(alist, testfile)
        pickle.dump(atuple,testfile)
        pickle.dump(adict,testfile)
    with open(filename, "rb+") as testfile:
        pickle_alist = pickle.load(testfile)
        pickle_atuple = pickle.load(testfile)
        pickle_adict = pickle.load(testfile)
        print(pickle_alist, pickle_atuple, pickle_adict)


testfileops(r'f:\pythonfiletest\testfile')

猜你喜欢

转载自blog.csdn.net/finalkof1983/article/details/88766160