python基础(十四)--文件操作

1,文件操作。
    模特主妇护士老师.txt
    1,文件路径:d:\模特主妇护士老师.txt
    2,编码方式:utf-8 gbk 。。。。
    3,操作方式:只读,只写,追加,读写,写读.....
        以什么编码方式储存的文件,就以什么编码打开进行操作。

只读:r
          rb
        f = open(‘模特主妇护士班主任‘,mode=‘r‘,encoding=‘utf-8‘)
        content = f.read()
        print(content,type(content))
        f.close()

# f = open(‘模特主妇护士班主任‘,mode=‘rb‘,)
        # content = f.read()
        # print(content)
        # f.close()

r+ 读写
        r+b 读写(以bytes类型)
        # f = open(‘log‘,mode=‘r+‘,encoding=‘utf-8‘)
        # print(f.read())
        # f.write(‘大猛,小孟‘)
        # f.close()

f = open(‘log‘,mode=‘r+b‘)
        print(f.read())
        f.write(‘大猛,小孟‘.encode(‘utf-8‘))
        f.close()

只写:w
          wb
    # 先将源文件的内容全部清除,在写。
    # f = open(‘log‘,mode=‘w‘,encoding=‘utf-8‘)
    # f.write(‘附近看到类似纠纷‘)
    # f.close()

f = open(‘log‘,mode=‘wb‘)
    f.write(‘附近看到类似纠纷‘.encode(‘utf-8‘))
    f.close()
    w+
    # f = open(‘log‘,mode=‘w+‘,encoding=‘utf-8‘)
    # f.write(‘aaa‘)
    # f.seek(0)
    # print(f.read())
    # f.close()
    w+b
    .......

追加
        # f = open(‘log‘,mode=‘a‘,encoding=‘utf-8‘)
        # f.write(‘佳琪‘)
        # f.close()

# f = open(‘log‘,mode=‘ab‘)
        # f.write(‘佳琪‘.encode(‘utf-8‘))
        # f.close()

#绝对路径
# f = open(‘d:\模特主妇护士班主任.txt‘,mode=‘r‘,encoding=‘UTF-8‘)
# content = f.read()
# print(content)
# f.close()

#bytes ---->str
# f = open(‘模特主妇护士班主任‘,mode=‘r‘,encoding=‘utf-8‘)
# content = f.read()
# f.write(‘fjsdlk‘)
# f.close()

# f = open(‘模特主妇护士班主任‘,mode=‘rb‘,)
# content = f.read()
# print(content)
# f.close()
# f = open(‘log‘,mode=‘r+‘,encoding=‘utf-8‘)
# print(f.read())
# f.close()

# f = open(‘log‘,mode=‘r+b‘)
# print(f.read())
# f.write(‘大猛,小孟‘.encode(‘utf-8‘))
# f.close()

#对于w:没有此文件就会创建文件
# f = open(‘log‘,mode=‘w‘,encoding=‘utf-8‘)
# f.write(‘骑兵步兵‘)
# f.close()

# 先将源文件的内容全部清除,在写。
# f = open(‘log‘,mode=‘w‘,encoding=‘utf-8‘)
# f.write(‘附近看到类似纠纷‘)
# f.close()

# f = open(‘log‘,mode=‘w+‘,encoding=‘utf-8‘)
# f.write(‘aaa‘)
# f.seek(0)
# print(f.read())
# f.close()

# f = open(‘log‘,mode=‘wb‘)
# f.write(‘附近看到类似纠纷‘.encode(‘utf-8‘))
# f.close()

# f = open(‘log‘,mode=‘a‘,encoding=‘utf-8‘)
# f.write(‘佳琪‘)
# f.close()
#
# f = open(‘log‘,mode=‘a‘,encoding=‘utf-8‘)
# f.write(‘佳琪‘)
# f.close()

# f = open(‘log‘,mode=‘a+‘,encoding=‘utf-8‘)
# f.write(‘佳琪‘)
# f.seek(0)
# print(f.read())
# f.close()

# f = open(‘log‘,mode=‘ab‘)
# f.write(‘佳琪‘.encode(‘utf-8‘))
# f.close()

#功能详解

# obj = open(‘log‘,mode=‘r+‘,encoding=‘utf-8‘)
# content = f.read(3)  # 读出来的都是字符
# f.seek(3)  # 是按照字节定光标的位置
# f.tell() 告诉你光标的位置
# print(f.tell())
# content = f.read()
# print(content)
# f.tell()
# f.readable()  # 是否刻度
# line = f.readline()  # 一行一行的读
# line = f.readlines()  # 每一行当成列表中的一个元素,添加到list中
# f.truncate(4)
# for line in f:
#     print(line)
# f.close()

# f = open(‘log‘,mode=‘a+‘,encoding=‘utf-8‘)
# f.write(‘佳琪‘)
# count = f.tell()
# f.seek(count-9)
# print(f.read(2))
# f.close()

# with open(‘log‘,mode=‘r+‘,encoding=‘utf-8‘) as f,\
#         open(‘log‘,mode=‘w+‘,encoding=‘utf-8‘) as f1:

username = input(‘请输入你要注册的用户名:‘)
password = input(‘请输入你要注册的密码:‘)
with open(‘list_of_info‘,mode=‘w‘,encoding=‘utf-8‘) as f:
    f.write(‘{}\n{}‘.format(username,password))
print(‘恭喜您,注册成功‘)
lis = []
i = 0
while i < 3:
    usn = input(‘请输入你的用户名:‘)
    pwd = input(‘请输入你的密码:‘)
    with open(‘list_of_info‘,mode=‘r+‘,encoding=‘utf-8‘) as f1:
        for line in f1:
            lis.append(line)
    if usn == lis[0].strip() and pwd == lis[1].strip():
        print(‘登录成功‘)
        break
    else:print(‘账号和密码错误‘)
    i+=1

#str --->byte  encode 编码
# s = ‘二哥‘
# b = s.encode(‘utf-8‘)
# print(b)
# #byte --->str decode 解码
# s1 = b.decode(‘utf-8‘)
# print(s1)

# s = ‘abf‘
# b = s.encode(‘utf-8‘)
# print(b)
# #byte --->str decode 解码
# s1 = b.decode(‘gbk‘)
# print(s1)

复习内容

# 文件处理
    # 打开文件
        #open(‘路径‘,‘打开方式‘,‘指定编码方式‘)
        # 打开方式 r w a r+ w+ a+ b
            #r+ 打开文件直接写 和读完再写
        # 编码方式 —— utf-8
    # 操作文件
        # 读
            # read 一次性读
            # readlines 一次性读
            # readline 一行一行读
                #不知道在哪儿结束
                #视频 图片 rb bytes 按照字节读
            # for循环 —— 最好!!!
        # 写
            # write
        # 光标 —— 文件指针
            #seek _ 指定光标移动到某个位置
            #tell _ 获取光标当前的位置
            #truncate _ 截取文件
    # 关闭文件
        #close

# 修改文件
with open(‘小护士班主任‘,encoding=‘utf-8‘) as f,open(‘小护士班主任.bak‘,‘w‘,encoding=‘utf-8‘) as f2:
    for line in f:
        if ‘星儿‘ in line:  #班主任:星儿
            line = line.replace(‘星儿‘,‘啊娇‘)
        #写文件
        f2.write(line) #小护士:金老板

import os
os.remove(‘小护士班主任‘) #删除文件
os.rename(‘小护士班主任.bak‘,‘小护士班主任‘)  #重命名文件

原文地址:https://www.cnblogs.com/qingmuzi/p/12652412.html

时间: 2024-08-28 13:55:51

python基础(十四)--文件操作的相关文章

python学习笔记-(七)python基础--集合、文件操作&amp;函数

本节内容 1.集合操作 2.文件操作 3.字符编码与转码 4.函数操作 1.集合操作 集合是一个无序的.不重复的数据组合: 1.1 常用操作 它的作用是: 1)自动去重:列表变成集合,自动去重: 1 2 3 4 >>> list_1 = [1,4,4,5,6,7,9,10] >>> list_1 =set(list_1) >>> print(list_1) {1, 4, 5, 6, 7, 9, 10} 2)关系测试:测试两组数据之间的关系,交集.并集.

Python 第十三节 文件操作

A 1.首先文件读写操作有以下几种模式:   a\a+  w\w+ r\r+   a模式:追加_写入模式,写入指针默认在开头,如果文件存在将在开头追加写入,如果文件不存在将创建文件再写入. a+模式:追加_读写模式,可读可写,写入指针默认在末尾,如果文件存在将在末尾追加写入,如果文件不存在将创建文件再写入. w模式:写模式,如果文件存在,把文件覆盖再写入,如果文件不存在将创建文件再写入. w+模式:写读模式,可写可读,如果文件存在,把文件覆盖再写入,如果文件不存在将创建文件再写入. r模式:读模

【python基础】之文件操作

一.文件操作的基本流程 #打开文件,得到文件句柄并赋值给一个变量 f = open('小重山','r',encoding='utf-8') #通过句柄对文件进行操作 print(f.read()) #关闭文件 f.close() 二.文件打开模式 r,只读模式(默认).w,只写模式.[不可读:不存在则创建:存在则删除内容:]a,追加模式.[可读: 不存在则创建:存在则只追加内容] "+" 表示可以同时读写某个文件 r+,可读写文件 [可读:可写:可追加] :光标默认在0位置,最后位置开

Python 基础语法(四)

Python 基础语法(四) --------------------------------------------接 Python 基础语法(三)-------------------------------------------- 十.Python标准库 Python标准库是随Pthon附带安装的,包含了大量极其有用的模块. 1. sys模块 sys模块包含系统对应的功能 sys.argv ---包含命令行参数,第一个参数是py的文件名 sys.platform ---返回平台类型 sy

Python基础知识(四)

Python基础知识(四) 一丶列表 定义格式: 是一个容器,由 [ ]表示,元素与元素之间用逗号隔开. 如:name=["张三","李四"] 作用: 存储任意类型的数据 (32位机器能存5亿多,64为机器存储更多) 特点: 可变 (增,删,改,查) 默认从左到右 ,从0开始 . 有序(索引,切片,步长) 操作: 增 , 删 , 改 ,查 ,索引,切片,步长 ?? #列表的两种定义方式 name=["香蕉","西瓜",&quo

python基础知识六 文件的基本操作+菜中菜

基础知识六 文件操作 ? open():打开 ? file:文件的位置(路径) ? mode:操作文件模式 ? encoding:文件编码方式 ? f :文件句柄 f = open("1.txt",mode = 'r',encoding = 'utf-8') print(f.read()) f.close 1.文件操作模式: ? r,w,a(重要) ? rb,wb,ab(次要) ? r+,w+,a+ 1.1 r/w/a 1. r操作: f = open('1.txt','r') pri

Python基础之一:文件类型及运算符

一.PYTHON文件类型 1.源代码 Python源代码的文件以"py"为扩展名,由Python解释,不需要编译: 2.字节代码 Python源文件经编译后生成的扩展名为"pyc"的文件: 编译方法:     importpy_compile     py_compile.compile("hello world.py") 3.优化代码 经过优化的源文件,扩展名为".pyo"  python –O –m py_compile 

初学 Python(十四)——生成器

初学 Python(十四)--生成器 初学 Python,主要整理一些学习到的知识点,这次是生成器. # -*- coding:utf-8 -*- ''''' 生成式的作用: 减少内存占有,不用一次性 创建list中所有的元素,而 是在需要的时候创建 ''' #创建generator有2种方式 #第一种将列表表达式中的[]改为()即可 g = (x*x for x in range(10)) print g for n in g: print n #第二种,关键字yield def fab(ma

python字符串处理与文件操作

1.strip函数 strip是trim(裁剪)掉字符串两边的空格或字符.(lstrip/rstrip) 如: 空格 theString = ' abcdbcyesornoabcbacdd ' print theString.strip() abcdbcyesornoabcbacdd 字符 theString = 'abcdbcyesornoabcbacdd' print theString.strip('abcd') #去掉两端的abcd字符 yesorno 问题:如何去掉中间空格. theS

Bootstrap&lt;基础十四&gt; 按钮下拉菜单

使用 Bootstrap class 向按钮添加下拉菜单.如需向按钮添加下拉菜单,只需要简单地在在一个 .btn-group 中放置按钮和下拉菜单即可.也可以使用 <span class="caret"></span> 来指示按钮作为下拉菜单. 下面的实例演示了一个基本的简单的按钮下拉菜单: <!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 基本的按钮下拉菜单&