文档username.txt

将文件中密码123456改成67890:

方法一:(简单粗暴)

  1.打开文件

  2.读出数据

  3.修改数据

  4.清空原来文件,将新的内容写进去

f = open('username','a+')

f.seek(0)

all_str = f.read()

new_str = all_str.replace('123456','67890')

f.seek(0)

f.truncate()  #清空文件

f.write(new_str)

f.colse()

方法二:(简单高效)

思路:

  1.打开一个文件a

  2.写一行写到b文件

  3.a.txt   a.txt.bat

  4.删掉a文件,b文件名字改成a文件名

将文件中‘笔记’替换为‘BiJi’

import os   #导入os模块

with open('words.txt') as fr,with open('.words.bak','w') as fw:

  for line in fr:

    new_line = line.replace('笔记','BiJi')

    fw.write(new_line)

os.remove('words.txt')   #删除文件

os.rename('.words.bak','words.txt')   #改名

python修改文件的更多相关文章

  1. python 修改文件内容

    python 修改文件内容 一.修改原文件方式 1 def alter(file,old_str,new_str): 2 """ 3 替换文件中的字符串 4 :param ...

  2. python 修改文件中的内容

    在python的文件操作中,是没有办法对文件中具体某行或者某个位置的内容进行局部的修改的,如果需要对文件的某一行内容进行修改,可以先将文件中的所有的内容全部读取出来,再进行内容判断,是否是需要修改的内 ...

  3. python 修改文件内容3种方法

    原文链接:https://www.cnblogs.com/wc-chan/p/8085452.html def alter(file,old_str,new_str): ""&qu ...

  4. python 修改文件的创建时间、修改时间、访问时间

    目录 python 修改文件创建.修改.访问时间 方案一 方案二(无法修改文件创建时间) python 修改文件创建.修改.访问时间 突如其来想知道一下 python 如何修改文件的属性(创建.修改. ...

  5. Python修改文件的两种方法

    目录: 一.以占用内存的方式修改文件 二.以占用硬盘的方式修改文件 引言 文件修改的方法从操作方式上大致可以分为两类,一种是以占用电脑内存的方式,将文件读取到内存中修改再存回硬盘:第二种方法是分别打开 ...

  6. Python修改文件内容

    工作中要写个脚本来修改文件的内容,然后就写了一个刷子: #coding:utf8 import os def modify_file(old_file, new_version, old_versio ...

  7. python修改文件后缀名

    修改文件后缀名 # -*- coding: utf-8 -*- import os # # 列出当前目录下所有的文件 # filedir = 'C:\\Users\\WT\\Desktop\\test ...

  8. python修改文件中字符串并写入

    python实际工作中,做一些小工具,很方便.最近在做一个格式转换工具时候,用到了替换文件中特定字符串的 功能.当初没直接想出来,就在网上查了一下,做个记录,方便后续使用. # -*- coding: ...

  9. python 修改文件编码方式

    import chardet import os def strJudgeCode(str): return chardet.detect(str) def readFile(path): try: ...

  10. python修改文件的属性

    1.执行attrib系统命令 ATTRIB [+R | -R] [+A | -A ] [+S | -S] [+H | -H] [+I | -I] [drive:][path][filename] [/ ...

随机推荐

  1. 关于JSON.parse(JSON.stringify(obj))实现深拷贝应该注意的坑

    JSON.parse(JSON.stringify(obj))我们一般用来深拷贝,其过程说白了 就是利用JSON.stringify 将js对象序列化(JSON字符串),再使用JSON.parse来反 ...

  2. APP元素定位工具weditor

    github地址https://github.com/openatx/weditor python -m weditor --shortcut adb devices 在页面上输入手机设备号,点Con ...

  3. DEV控件的分页控件,实现勾选复选框

    /// <summary> /// 单元格的点击事件 /// </summary> /// <param name="sender"></ ...

  4. java上传文件-大文件以二进制保存到数据库

    转自:https://blog.csdn.net/qq_29631069/article/details/70054201 1 一.创建表 oracle: create table baoxianda ...

  5. linux NFS 实例

    为了证明是 Allentunsgroup 组起的作用而非用户 [root@NFS_Client ~]# useradd scott1 [root@NFS_Client ~]# passwd scott ...

  6. 北京师范大学第十五届ACM决赛-重现赛E Euclidean Geometry (几何)

    链接:https://ac.nowcoder.com/acm/contest/3/E 来源:牛客网 Euclidean Geometry 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ ...

  7. 计蒜客 蓝桥模拟 H. 封印之门

    Floyd算法,最短路,判断a,b是否相等. 代码: #include <cstdio> #include <cstdlib> #include <cstring> ...

  8. 1118. Birds in Forest (25)

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

  9. php内置函数分析之strpos()

    PHP_FUNCTION(strpos) { zval *needle; zend_string *haystack; char *found = NULL; ]; zend_long offset ...

  10. Flutter-常用插件庫

    alibaba/flutter_boost:路由 install_plugin 2.0.0#app下载更新插件 audio_recorder: any #录音.播放 flutter_sound: ^1 ...