读写行
    readline():会从文件中读取单独一行,换行符为\n,readline方法如果返回一个空字符串,说明已经读取到最后一行。
    readlines()方法可以传入参数,当传入的数值小于等于列表中一个字符串的长度值时,该字符会被读取
    图片.png

    1. path='C:/Users/Administrator/Documents/test.txt'
    2. f_name=open(path,'w')
    3. str_list=['hello world!\n welcome!\n welcome!\n']
    4. print('write length',f_name.writelines(str_list))
    5. f_name=open(path,'r')
    6. print('read result',f_name.read())
    7. f_name=open(path,'r')
    8. print('readlines result',f_name.readlines())
    9. #关闭文件
    10. path='C:/Users/Administrator/Documents/test.txt'
    11. with open(path,'w')as f:
    12. f_name=open(path,'w')
    13. print('write length:',f_name.write('hello world'))
    14. #文件重命名 文件名需要带上绝对路径
    15. import os
    16. path='C:/Users/Administrator/Documents/test1.txt'
    17. os.rename('C:/Users/Administrator/Documents/test1.txt','C:/Users/Administrator/Documents/test2.txt')
    18. #删除文件 os.remove(file_name) 文件名需要带上绝对路径
    19. import os
    20. try:
    21. print('remove result:',os.remove('C:/Users/Administrator/Documents/test2.txt'))
    22. except Exception:
    23. print('file not found')

    对文件内容进行迭代

    1. #文件内容进行迭代
    2. path='C:/Users/Administrator/Documents/test2.txt'
    3. f_name=open(path,'w')
    4. print('write length:',f_name.write('hello'))
    5. f_name=open(path)
    6. c_str=f_name.read(1)
    7. while c_str:
    8. print('read str is :',c_str)
    9. c_str=f_name.read(1)
    10. #优化代码
    11. path='C:/Users/Administrator/Documents/test2.txt'
    12. f_name=open(path)
    13. while True:
    14. c_str=f_name.read(1)
    15. if not c_str:
    16. break
    17. print('read str is:',c_str)
    18. f_name.close()
    19. #按行进行迭代
    20. f_name=open(path)
    21. while True:
    22. line=f_name.readline(1)
    23. if not c_str:
    24. break
    25. print('readline is:',line)
    26. f_name.close()
    27. #使用fileinput实现懒加载式迭代
    28. #read和readlines方法不带参数时读取文件内容加载到内存中,当文件很大时会占用太多内存
    29. #使用for循环,则称为懒加载式迭代,只读取实际需要的文件部分
    30. import fileinput
    31. for line in fileinput.input(path):
    32. print('line is :',line)
    33. #该例子的文件打开和关闭操作封装在input内部。
    34. #对str操作的StringIO函数
    35. from io import StringIO
    36. io_val=StringIO()
    37. io_val.write('hello')
    38. print('say:',io_val.getvalue())#getvalue()用于获得写入后的str
    39. #对str操作的StringIO函数
    40. from io import StringIO
    41. io_val=StringIO('hello world!\nwelcome\n')
    42. while True:
    43. line=io_val.readline()
    44. if line=='':
    45. break
    46. print('line value:',line.strip())

    序列化和反序列化:
    在程序运行的过程中,所有的变量存在内存中过,我们把变量从内存中变成可存储的或可传输的过程称为序列化。
    反之,把变量内容从序列化的对象重新读到内存中称为反序列化。
    序列化是指将数据结构或对象转换成二进制串的过程。

    1. #pickle.dump(obj,file,[,protocol])
    2. import pickle
    3. d=dict(name='xiaomi',num='1010')
    4. print(pickle.dumps(d))#pickle.dumps()方法把任意对象序列转化成bytes,然后写入文件。
    5. #pickle.dump()方法是把任意对象序列转化成bytes,然后写入对象中。
    6. #pickle.dump(obj,file,[,protocol])
    7. import pickle
    8. d=dict(name='xiaomi',num='1010')
    9. print(pickle.dumps(d))
    10. #程序
    11. try:
    12. f_name=open('C:/Users/Administrator/Documents/test2.txt','wb')
    13. pickle.dump(d,f_name)
    14. finally:
    15. f_name.close()

    调试:repr函数
    制表符、换行符和空格都是看不见的

    1. str_val='a 2\t 3\n 4 5'
    2. print(str_val)
    3. print(repr(str_val))#把字符原本输出,方便调试

    图片.png