【发布时间】:2020-05-03 00:08:13
【问题描述】:
我有一个txt文件,配置如下:
---01---2125---1648---
random words
random words
---01---3068---1648---
random words
random words
---02---3068---1648---
random words
random words
目标是创建一个目录,并根据其所属目录在该目录中插入文件。所有这些都使用文件 data_base.txt 中给出的名称。示例:
data_base
|_01/
| |_2125.txt
| |_3068.txt
|
|_02/
| |_3068.txt
|
等等... txt 文件将用它们各自的随机词填充。代码如下:
import glob, os
from os.path import isfile
path = "/Users/text"
os.mkdir(path)
fp = open("data_base.txt", 'r', -1)
codes = fp.readlines()
for i in codes:
print(i.replace("---"," ").strip())
我能够使用替换功能删除数字之间的字符串“---”。问题是我没有通过搜索将是目录名称的字符串(01、02 等)来正确理解它。
有谁知道如何将相应的字符串插入为目录名称?
【问题讨论】:
-
Can someone help me? -- 您的最后一句话不是适合 Stack Overflow 问题的重点。请您根据您的具体需要重新措辞一下吗?
-
@Prune 好的,这个问题已经足够了。欣赏。