python如何将文件a.txt的内容复制到b.txt中
作者:LanLanDeMing 时间:2021-04-24 15:22:33
python将a.txt的内容复制到b.txt 中
# 1. 用r方式打开a.txt
f = open("a.txt","r")
# 2. 读取a.txt的内容
buf = f.read()
# 3. 关闭a.txt
f.close
# 4. 用w或者a的方式打开b.txt
f = open("b.txt","w")
# 5. 将文件写入b.txt中
f.write(buf)
# 6. 关闭b.txt
f.close()
python合并多个txt中的内容到一个txt中
在使用python进行合并多个txt的内容时,使用如下:
with open(filepath,'w') as f:
f.write(line)
会导致将原来的filepath中的内容进行覆盖,如何进行不覆盖形式的编写呢,我们可以进行先读行再写的方式(先readlines(),后write),源代码如下,同时还包含将文章中的符号、数字等的去除:
import os
import re
import sys
mefile_name=['D:\\LDA\\LdaStemDocs2\\breast cancer\\','D:\\LDA\\LdaStemDocs2\\colon cancer\\','D:\\LDA\\LdaStemDocs2\\lung cancer\\','D:\\LDA\\LdaStemDocs2\\NHL\\','D:\\LDA\\LdaStemDocs2\\pancreatic cancer\\','D:\\LDA\\LdaStemDocs2\\prostate cancer\\','D:\\LDA\\LdaStemDocs2\\urinary bladder cancer\\']
for i in range(len(mefile_name)):
for j in range(len(datil_name)):
meragefiledir=mefile_name[i]+datil_name[j]
filenames=os.listdir(meragefiledir)
filestore_name='\\result_.txt'
with open(filestore_name,'w',encoding='utf-8') as f:
for filename in filenames:
filepath=meragefiledir+'\\'
filepath=filepath+filename
for line in open(filepath).readlines():
line=re.findall(r'[a-zA-Z]+\s+[a-zA-Z\s]*',line)
line="".join(line)
#print(line)
f.write(line+'\n')
#f.write('\n')
print('combine {0}{1} ending~~~~~~\n'.format(mefile_name[i],datil_name[j]))
print('ending all')
以上代码删除了原有的open(file)路径,具体根据自己的文件路径进行添加。
来源:https://blog.csdn.net/LanlanDeming/article/details/103226087
标签:python,a.txt,复制,b.txt
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
php中json_decode()和json_encode()的使用方法
2023-06-11 16:31:56
浅谈Webpack多页应用HMR卡住问题
2023-07-20 01:27:12
![](https://img.aspxhome.com/file/2023/5/55925_0s.jpg)
php中in_array函数用法探究
2023-06-21 21:03:54
django_orm查询性能优化方法
2021-03-01 20:30:19
多个python文件调用logging模块报错误
2021-03-30 23:30:24
ASP+SQL Server构建网页防火墙
2009-01-21 19:56:00
Python 结巴分词实现关键词抽取分析
2023-08-13 08:20:06
![](https://img.aspxhome.com/file/2023/6/107196_0s.png)
关于python变量的引用以及在底层存储原理
2021-06-24 07:37:35
python检测空间储存剩余大小和指定文件夹内存占用的实例
2022-10-30 06:52:51
Python中表达式x += y和x = x+y 的区别详解
2022-02-24 04:11:19
Google投放广告的js的分析
2008-07-15 11:34:00
Python CSV 文件解析和生成方法示例
2022-12-25 10:52:58
Python中关键字global和nonlocal的区别详解
2023-08-02 16:42:33
![](https://img.aspxhome.com/file/2023/7/60957_0s.png)
python如何实现数组元素两两相加
2023-02-21 04:39:46
Python tkinter常用操作代码实例
2021-01-05 21:26:09
django使用JWT保存用户登录信息
2022-02-19 17:36:58
![](https://img.aspxhome.com/file/2023/4/107244_0s.png)
使用keras实现孪生网络中的权值共享教程
2022-10-21 13:52:34
![](https://img.aspxhome.com/file/2023/3/124403_0s.jpg)
Python实现测试磁盘性能的方法
2022-01-31 19:00:46
详解python函数传参传递dict/list/set等类型的问题
2021-09-29 16:12:38
Python 使用dict实现switch的操作
2021-12-28 02:47:52
![](https://img.aspxhome.com/file/2023/2/115642_0s.jpg)