網站首頁 編程語言 正文
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
相關推薦
- 2022-05-22 C#多線程的相關操作講解_C#教程
- 2022-11-05 WPF+ASP.NET?SignalR實現后臺通知功能的示例代碼_C#教程
- 2022-05-31 python應用之如何使用Python發送通知到微信_python
- 2022-04-15 ASP.NET?Core基礎之Startup類_基礎應用
- 2022-05-11 mac ssh 免密登錄 localhost
- 2022-02-27 selectByPrimaryKey()按主鍵查詢結果為null
- 2023-11-11 Python測網絡連通性、能否訪問某個網絡或者端口號<網絡檢測、ping主機、測試端口>
- 2023-02-18 Go語言IO輸入輸出底層原理及文件操作API_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支