python文件操作
发布日期:2021-05-14 21:58:11 浏览次数:12 分类:博客文章

本文共 16089 字,大约阅读时间需要 53 分钟。

python������������

  • ������������
  • ������������������
  • ������������

������������

���������������������������������������������������������������������������

@requires_authorization# ������������������a = 2b = 5if a < b:  # if������������    val = aelse:    val = bprint(val)  # val = 2# ���������������������val = a if a < b else bprint('val:',val)  # val: 2

������������������

������������

������������������������������������������Python���������������������������������������������������������������������������

������������������������������

ASCII       ���1���������       ���������������GB2312      ���2���������       ������6700+������GBK         GB2312������������  ������21000+������Shift_JIS           ������������ks_c_5601_1987      ������������TIS-620             ������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

  • Unicode 2-4 ���������������������������136690������������������������������������

Unicode���������2������������

  • ������������������������������������������������������������������������������������������������Unicode������������
  • Unicode���������������������������������������������������

Unicode���������������������������������������������������������Unicode������������������������������������������������������Unicode������"python"������12���������������������������������ASCII���������������1������

������������������������������������������������������������������������������������������������������������Unicode������������������������������������������������������������������������������������1������������������������

������������������������������������������������������UTF������������Unicode������������������������������������������������������������������������������

  • UTF-8���������1-4���������������������������������������������1������������������������������������1������������������4���������������������1��������� ������������������2���������������3������������������������������4������
  • UTF-16���������2-4������������������������������������������2������������������������4������������������
  • UTF-32���������4���������������������������������

���������UTF���������Unicode������������������������������������������������������������������������

������������

������

������1��������������������� "������������.txt" ������������������������

������

1.���������������������������

2.������������������������������������������������

3.������ or ������

4.���������������

������2������������������������������������������

������

������������������������"01010101010101"���������������

python������������

python������������������������������

���������

���������pycharm������ "������������.txt" ������������

������	������	������������������	23	13003123113������	20	18947123319������	25	13888889888������	18	18492134132

������������������������������������������������������������������

������1���

f = open('������������.txt',mode='r',encoding='UTF-8')  data = f.read()  print(data)f.close()  '������������.txt' ���������������������������������������������������������������������������mode = 'r' ������������������encoding = 'UTF-8' ���������������������01010101������UTF-8������������������������������������������������������0101010���������unicode��� 01010101���unicode���������������01010101���������������������������f.read() ������������������������������������������������������������������f.close() ������������������# ������������������������������������������������������������������������������������������# ���������������������������f = open(file = 'D:\py_study\day06-������������\������������.txt',mode = 'r',encoding='utf-8')data = f.read()print(data)f.close()���������������������    ���������������������������������������������������������������������������������������������������������������������������������������������������������    ������������������������������������������������������������������������������������

������2���

f = open('������������.txt','rb')data = f.read()print(data)  # ������������16������f.close()'������������.txt'  # ���������������������������������������������������������������������������������mode = 'rb'  # ������������f.read()  # ��������������������������������������������������������������������������� 010101010������������������������������������������f.close()  # ������������������

���������������������

������	������	������������������	23	13003123113������	20	18947123319������	25	13888889888������	18	18492134132

������������1���������2���������������������

������������2���������������encoding������������������������������������rb���������������������������rb������������������������������������������������������bytes������������������������������������������decode���������������������������������������������������������

���������������������������������������������������������������

import chardet  # ���������������f = open('������������.txt',mode='rb')  # ������������������������data = f.read()f.close()result = chardet.detect(open('������������.txt',mode = 'rb').read())  # ������������������������detect���������������print(result)

������������������

{'encoding': 'utf-8', 'confidence': 0.99, 'language': ''}  # ���99%���������utf-8

���������

  • ���������������������r������rb���������������������������������������������
  • ���������������������������������������������0101010���������������������������
    • rb���������������������������������������01010101,���python������������������������
    • r���encoding���������������������01001010������������encoding���������������������������������������������������������������0101001���������unicode���01010101101,���python���������������������������

������������

f = open('������������.txt','r',encoding='utf-8')for line in f:    print(line)  # ������������f.close()

���������������������

������  ������  10402384103������  ������  10403145103������  ������  13888888888������  ������  12389874589������  ������  10123451313

���������

f = open('���������.txt','w',encoding='utf-8')f.write('������������������������������50���������������xxxxx')f.close()

���������������������������

'���������.txt'  # ������������������mode = 'w'  # ������������encoding='utf-8'  # ���������������unicode���������������utf-8������f.write()  # ������������������������������������������unicode���������������������������������encoding������������������������ 01101010101���������������������f.close()  # ������������

������������

# ������������f = open('���������.txt',mode='wb')f.write(' hello python'.encode('utf-8'))f.close()

���������

1������������������������w������wb������������������������������������������������������������������������������

2���������������������������������������������������0101010������������������������

  • wb,���������������������������������������������01010100���������������������
  • w���encoding������������������������unicode���������������������������encoding������������������unicode������������������������������01010101

������

������������������������������

f = open('������������.txt','a',encoding='utf-8')f.write('\n���������  ������  13004562134')f.close()

���������������

������  ������  10402384103������  ������  10403145103������  ������  13888888888������  ������  12389874589������  ������  10123451313���������  ������  13004562134  # ���������������������

���������

1������������������������a���ab������������������������������������������������������������������������������

2���������������������������������������������������01010101���������������������������

  • ab���������������������������������������������01010101���������������������
  • a���encoding,���������������������unicode���������������������������encoding������������������unicode������������������������������ 010101010

������������

������������

���������������������������������������������

f = open('������������.txt','r+',encoding='utf-8')data = f.read()print(data)f.write('\n ��������� ������  12345678123')f.close()

���������������������

��������� ������  12345678123 ��������� ������  12345678123 ��������� ������  12345678123 ��������� ������  12345678123 ��������� ������  12345678123 ��������� ������  12345678123

������������

������������������������������������������������������

f = open('���������.txt',mode='w+',encoding='utf-8')f.write(' ��������� ������  12345678123')f.close()

���������������������

��������� ������  12345678123

���������������������������

def fileno(self, *args, **kwargs): # real signature unknown        ������������������������������������������������������IO���������������������������    def flush(self, *args, **kwargs): # real signature unknown        ������������������buffer������������������������    def readable(self, *args, **kwargs): # real signature unknown        ������������������    def readline(self, *args, **kwargs): # real signature unknown        ���������������������\r or \n������    def seek(self, *args, **kwargs): # real signature unknown        ������������������������������������������        *������seek������������������������������ ������������������������������������������������������������        ������������������ ���gbk������2������������������������utf-8������3���������������������gbk������������seek(4) ���������������������������������������������������������������        ������������utf8,seek(4)���������������������������������������������������������������������������������������������������������������������utf8���������������������������������������������������������������    def seekable(self, *args, **kwargs): # real signature unknown        ���������������������������seek������    def tell(self, *args, **kwargs): # real signature unknown        ������������������������������������     def truncate(self, *args, **kwargs): # real signature unknown        ���������������������������        *������������������������������������������������������������������������������������������������������������������������������������������������    def writable(self, *args, **kwargs): # real signature unknown        ������������������������

flush()������������������buffer������������������������

T>>> f = open('flush.txt','w',encoding='utf-8')>>> f.write('xiaoyafei')  # ������������������������������������������������������������������������������9>>> f.flush()  # ������������������������������������������>>> f.close()

readable()������������������������������������True

f = open('1.txt','r',encoding='utf-8')print(f.readable())  # Truef.close()

readline()���������������������\r or \n������

# ������������.txt������-������-������    1������-������-������    1

���������

f = open('������������.txt','r',encoding='utf-8')data = f.readline()print(data)  # ���������������  ������-������-������    1 f.close()

seek()������������������������������������������

tell()������������������������

# admin.txt  utf-8admin-admin-admin-1admin-admin-admin-2admin-admin-admin-3admin-admin-admin-4

������:

f = open('admin.txt','r',encoding='utf-8')print(f.tell())  # 0print(f.readline())  # admin-admin-admin-1print(f.tell())  # 21f.seek(2)  # ���������2print(f.readline())  # min-admin-admin-1

������

1.f.read()���������������

2.f.tell()���f.seek()���������������

seekable() ������������������������������seek������

# ���������admin.txt ���������������f = open('admin.txt','r',encoding='utf-8')print(f.seekable())  # Truef.close()

writeable() ������������������������������

f = open('admin.txt','r',encoding='utf-8')  # mode = 'r' ������������print(f.writable())  # Falsef.close()f = open('admin.txt','w',encoding='utf-8')  # mode = 'w' ���������print(f.writable())  # Truef.close()

truncate() ���������������������������,���������������������������

# f_flush.txt������������:���������

������:

f = open('f_flush.txt','r+',encoding='utf-8')res = f.read()print(res)  # ���������print(f.truncate(6))  # 6f.flush()  # ������f.close()

������������������������������������������:

# ������������.txt������������������     ������    173    50    13744234523������     ������    172    52    15823423525������     ������    175    49    18623423421������     ������    170    48    18623423765������     ������    172    52    15823423514������     ������    175    49    18623423311������     ������    170    48    18623423315

������:

import os  # ���������������f_name = '������������.txt'  # ���������������f_new_name = '%s.new' % f_name  # ���������������old_str = '���������'  # ������������������������new_str = '������'  # ���������...f = open(f_name,mode='r',encoding='utf-8')  # ���������������������������f_new = open(f_new_name,mode='w',encoding='utf-8')  # ���������������������������for line in f:  # ������������    if old_str in line:  # ������old_str���line���        line = line.replace(old_str,new_str)  # ������old_str���new_str    f_new.write(line)  # ������������������f.close()f_new.close()

������1:

���������������������seek,���������������������,������������������������������������,���������,���������������,���������������,������������������������������������,������������,������,���������������������������������������?

������     ������    173    50    13744234523������     ������    172    52    15823423525������     ������    175    49    18623423421������     ������    170    48    18623423765������     ������    172    52    15823423514������     ������    175    49    18623423311������     ������    170    48    18623423315

���������������������������������������?:

f = open('������������.txt','r+',encoding='utf-8')f.seek(6)f.write('xiaoyafeibaba')f.close()

������������:

������xiaoyafeibaba  173    50    13744234523������     ������    172    52    15823423525������     ������    175    49    18623423421������     ������    170    48    18623423765������     ������    172    52    15823423514������     ������    175    49    18623423311������     ������    170    48    18623423315

f.write()���������������6���������������������,���������������������������������������,������������������?

������������������������������������������������������������������������������������������������������������������������������������������������������ ���seek������������������������������������������������������������������������������������������������������������������������������������������������������������������������������ ������������������ ���������������������������������������������������������

������������word���vim���������������������?

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������vim word���������������������������������������������

������������������������������������������word���������������������������������������������

��������������������������������������� ������������������������word��������������� ������������������������������������������������������������������������word or vim������������������������������ ���������������MB��������� ��������������������������������������������������������������������� cpu ������������������������������ ������������������������������������ ������������������������������

������������������������������������5���GB,������������������������������������5GB���������������������������������������������������������

������������������������������������������������������������������������������ ��������������� ������������������������������������������ ������������������������������ ��������������������������������������������������������������������������������������������������������������������������������������������������� ������������������������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������������������������������������������������������������������������������������������������2��������� ������

上一篇:python函数基础
下一篇:ASCII、Unicode和UTF-8编码的区别

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年04月11日 12时28分50秒