python-3.x - 命名所有连接的 txt 文件
问题描述
我想将所有 .txt 文件中的数据收集到一个文件中,并在此连接文件中添加收集到的 .txt 名称的名称。我尝试用我的代码来实现它,但没有。如果您能提供帮助,我将不胜感激
资源:
import glob
read_files = glob.glob("*.txt") # find all .txt files and then goes read
fileName = 'NoName'
with open("result.txt", "wb") as outfile: # create output file named result.txt
for f in read_files:
fileName = f
with open(f, "rb") as infile:
outfile.write(str(fileName) + '\n' + infile.read()) # in this line i try to add at top off collected text 'Name' of collected .txt, but i got some errors ¯\_(ツ)_/¯
解决方案
尝试使用.decode("utf-8")
并更改wb
为w
前任:
import glob
read_files = glob.glob("*.txt") # find all .txt files and then goes read
with open(filename, "w") as outfile: # create output file named result.txt
for f in read_files:
with open(f, "rb") as infile:
outfile.write(f + '\n' + infile.read().decode("utf-8") )
推荐阅读
- qt - 修改 ListElement (QML) 内的工具提示文本
- php - php AD ldap 搜索过滤器特定 OUS
- django - Django:避免在模板中呈现为变量
- python - Pandas:将多索引级别转换为一组新的列
- python - pd.to_datetime 好的,但仍然不是情节中的日期
- android - 按钮未填充父容器高度
- python - 使用 pyinstaller 转换为 exe 时出现 Moviepy 模块错误
- azure - 在 azure blob 存储容器中,我可以使搜索功能(文件夹)不区分大小写吗
- css - CSS 视差:“溢出:隐藏;” 在 iOS 上打破 3D 变换/透视
- kotlin - 如何在 Kotlin 中使用 okhttp3 进行气流实验 api?