首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将列表保存为htb文件?

如何将列表保存为htb文件?
EN

Stack Overflow用户
提问于 2021-04-12 21:17:09
回答 1查看 15关注 0票数 0

我试图创建一个拉格雷htb文件的许多小的htb文件。到目前为止,我可以创建一个包含目录中所有文件内容的列表。如何再次将我的列表保存到htb文件?你们有人能帮我吗?到目前为止,我的代码如下:

代码语言:javascript
复制
import os

folderpath = r"C:/Users/l-reh/Desktop/HTB" 
filepaths  = [os.path.join("C:/Users/l-reh/Desktop/HTB/", name) for name in os.listdir(folderpath)]
all_files = []

for path in filepaths:
    with open(path, 'r') as f:
        file = f.readlines()
        all_files.append(file)
EN

回答 1

Stack Overflow用户

发布于 2021-04-12 21:49:53

可以使用itertools.chain.from_iterable()连接列表的嵌套列表

代码语言:javascript
复制
>>> from itertools import chain
>>> all_files = [["this is some", "content"], ["this", "is more stuff"], ["and", "more and", "more"]]
>>> newfile_contents = "\n".join(chain.from_iterable(all_files))
>>> print(newfile_contents)
this is some
content
this
is more stuff
and
more and
more
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67059105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档