zoukankan      html  css  js  c++  java
  • python文件操作

    1、文件操作

    1.1、对文件的读

     1 # 事先在E盘下创建一个文本文档并读取
     2 f = open('E:新建文本文档.txt',mode='r',encoding='ANSI')
     3 content = f.read()
     4 print(content)
     5 f.close()
     6 
     7 # 事先在项目下创建一个文本文档,再去读它
     8 f = open('demo',mode='r',encoding='utf-8')
     9 content = f.read()
    10 print(content)
    11 f.close()
    12 
    13 # 以二进制打开,这种读取方式主要用于非文字文件
    14 f = open('demo',mode='rb')
    15 content = f.read()
    16 print(content)
    17 f.close()

    1.2、对文件的写

     1 # 没有此文件就会创建文件,已有此文件就会将源文件中的内容删除,再写。
     2 # 注意写的时候光标在内容的前面
     3 f = open('demo1',mode='w',encoding='utf-8')
     4 f.write('只写,再添加')
     5 f.close()
     6 
     7 # wb
     8 f = open('demo2',mode='wb')
     9 f.write('只写,再添加'.encode('utf-8'))
    10 f.close()

    1.3、追加

    1 # 追加:a
    2 f = open('demo2',mode='a',encoding='utf-8')
    3 f.write(',追加')
    4 f.close()
    5 
    6 # 二进制追加:ab
    7 f = open('demo2',mode='ab')
    8 f.write(',二进制追加'.encode('utf-8'))
    9 f.close()

    1.4、读写操作

     1 # 先读,再写:r+
     2 f = open('demo2',mode='r+',encoding='utf-8')
     3 print(f.read())
     4 f.write(',先读,再写')
     5 f.close()
     6 
     7 # 在读写模式下,进行写读
     8 # 若demo下的内容为python,我先写入aaa,再进行读,会发现读出来的内容是hon,而demo下的内容是aaahon
     9 # 请记住,在读写模式下进行写读,写入的内容会从前往后,替换原有的内容,读出的内容是没有被替换的内容
    10 f = open('demo',mode='r+',encoding='utf-8')
    11 f.write('aaa')
    12 print(f.read())
    13 f.close()
    14 
    15 # 以二进制进行读写
    16 f = open('demo2',mode='r+b')
    17 print(f.read())
    18 f.write(',以二进制类型读写'.encode('utf-8'))
    19 f.close()

    1.5、写读操作

    # 正常的先写再读,虽然能写入内容,但是却读不出来,但是只要在写之后加一个f.seek(0),将光标移到最前面,就可以读出来了
    f = open('demo2',mode='w+',encoding='utf-8')
    f.write('写读')
    f.seek(0)
    print(f.read())
    f.close()
    
    # 以二进制进行写读
    f = open('demo2',mode='w+b')
    f.write('以二进制写读'.encode('utf-8'))
    f.seek(0)
    print(f.read())
    f.close()

    1.6、追加之后再读取

    # a是追加,追加之后对他进行读会报错
    f = open('demo2',mode='a',encoding='utf-8')
    f.write('追加')
    print(f.read())
    f.close()
    
    # 使用a+之后,对他进行追加,再将光标移动到0,再读就可以了
    f = open('demo2',mode='a+',encoding='utf-8')
    f.write('追加')
    f.seek(0)
    print(f.read())
    f.close()

    1.7、一些功能讲解

    1.7.1、截取源文件的字符

     1 f = open('demo2',mode='r+',encoding='utf-8')
     2 content = f.read(3) # 读出来的都是字符,读取前三个
     3 print(content)
     4 f.close()
     5 
     6 f = open('demo2',mode='r+',encoding='utf-8')
     7 f.seek(2) # 定位到第三个,读剩下的。
     8 content = f.read()
     9 print(content)
    10 f.close()

    1.7.2、对seek功能的讲解

    # 若demo2中的内容是中文,如:你好啊
    f = open('demo2',mode='r+',encoding='utf-8')
    f.seek(3) # result:好啊
    f.seek(2) # 会报错
    content = f.read()
    print(content)
    f.close()
    # 解释:seek定位是按照字节来定位的,一个中文三个字节,若seek(3)则正好定位到第二个字,若小于三,就定位不到第一个,就会报错

    1.7.3、tell()、readable()、readline()、readlines()、truncate()

    f = open('demo',mode='r+',encoding='utf-8')
    content = f.read()
    print(content)
    print(f.tell())# 得到当前光标的位置
    print(f.readable())# 判断是否可读
    print(f.readline())# 一行一行的读
    print(f.readlines())# 每一行当成列表中的一个元素,添加到list中
    f.truncate(2)# 从源文件中截取字符串,源文件会被改变
    f.close()

    1.7.4、对文件进行for循环

    1 对文件进行for循环
    2 f = open('demo',mode='r+',encoding='utf-8')
    3 for ln in f:
    4     print(ln)

    1.7.5、with open()

    1 # 写close可能有时候会忘记写,使用with open就不需要close
    2 with open('demo',mode='r+',encoding='utf-8') as f:
    3     print(f.read())
    4 
    5 # 同时打开多个进行操作
    6 with open('demo',mode='r+',encoding='utf-8') as f,
    7         open('demo',mode='r',encoding='utf-8') as f1:
    8     print(f.read())
    9     print(f1.read())

    1.7.6、登录注册功能的应用

     1 username = input('请输入用户名')
     2 password = input('请输入密码')
     3 with open('demo',mode='w',encoding='utf-8') as f:
     4     f.write('{}
    {}'.format(username,password))
     5 print('恭喜您注册成功!')
     6 lis = []
     7 i = 0
     8 while i < 3:
     9     usn = input('请输入用户名')
    10     pwd = input('请输入密码')
    11     with open('demo',mode='r+',encoding='utf-8') as f1:
    12         for ln in f1:
    13             lis.append(ln)
    14     if lis[0].strip() == usn and lis[1].strip() == pwd:
    15         print('恭喜您,登录成功')
    16         break
    17     else:print('账号密码错误')
    18     i += 1

    1.8、如何去修改文件,删除文件,重命名文件

        有一个文件,我想去修改他的内容,但是我想让别人能够看出来修改后的文件和修改之前的文件,那么我要怎么做呢?

     1 # with open('原文件','w',encoding='utf-8') as f:
     2 #     f.write('原文件,未修改')
     3 
     4 # 修改文件
     5 with open('原文件',encoding='utf-8') as f,open('现文件','w',encoding='utf-8') as f1: # open语句中,未指定操作方式,则默认为r,此外mode可以不写,直接写操作方式。
     6     for ln in f:
     7         if '未修改' in ln:
     8             ln = ln.replace('未修改','已修改')
     9         f1.write(ln)
    10 
    11 import  os
    12 # 删除原文件
    13 os.remove('原文件')
    14 # 重命名文件
    15 os.rename('现文件','原文件')
  • 相关阅读:
    把影响集中到一个点
    How to avoid Over-fitting using Regularization?
    适定性问题
    Numerical Differentiation 数值微分
    What Every Computer Scientist Should Know About Floating-Point Arithmetic
    Generally a good method to avoid this is to randomly shuffle the data prior to each epoch of training.
    What is the difference between iterations and epochs in Convolution neural networks?
    Every norm is a convex function
    Moore-Penrose Matrix Inverse 摩尔-彭若斯广义逆 埃尔米特矩阵 Hermitian matrix
    perl 类里的函数调用其他类的函数
  • 原文地址:https://www.cnblogs.com/missdx/p/10918601.html
Copyright © 2011-2022 走看看