【发布时间】:2021-08-18 12:46:36
【问题描述】:
这是我的代码
import os
pwd = os.getcwd()
playlist_links = ['A', 'B', 'C', 'D', 'E', 'F',]
for link in playlist_links:
# Create the path
path = os.path.join(pwd, link)
# Create the directory
if not os.path.exists(path):
os.mkdir(path)
os.chdir(path)
with open("Z:/www.rttv.com/Change_Into_Dir_TEST/single-video-v5-{}.txt".format(link), 'w', encoding="utf-8") as output_file:
output_file.write(link)
os.chdir(pwd)
这是我得到的输出:
我想要的是将每个 txt 文件放在其受尊重的文件夹中。例如:
single-video-v5-A.txt 应该在 A 文件夹中
single-video-v5-B.txt 应该在 B 文件夹中
single-video-v5-C.txt 应该在 C 文件夹中
等等等等…… 我的代码到底有什么问题?
我将非常感谢任何帮助。提前谢谢!
【问题讨论】:
-
您没有将占位符作为文件夹包含在路径中,而是将其放在文件名的末尾...
标签: python arrays loops directory with-statement