如何使用 python 将 txt 文件生成到单独的目录中?
How do I generate txt files into separate directories with python?
这是我的代码
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文件夹下
等等……
我的代码究竟有什么问题?
如有任何帮助,我将不胜感激。提前致谢!
你要看这个逻辑,变化很小但效果很关键:
import os
pwd = os.getcwd()
playlist_links = ['A', 'B', 'C', 'D', 'E', 'F',]
for link in playlist_links:
path = os.path.join(pwd, link)
if not os.path.exists(path):
os.mkdir(path)
os.chdir(path)
with open(f"single-video-v5-{link}.txt", 'w', encoding="utf-8") as output_file:
output_file.write(link)
os.chdir(pwd)
执行后,查看当前工作目录的内容,然后是A/目录的内容:
$ ls
A B C D E F p.py
$ ls A/
single-video-v5-A.txt
可能会有帮助
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)
save_file = f'single-video-v5-{link}.txt' # create the file name
save_path = os.path.join(path,save_file) # create the save_path using above file name
# open the file write it and close
with open(save_path, 'w', encoding="utf8") as out_file:
out_file.write(link)
我认为没有必要调用 os.chdir()
这是我的代码
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文件夹下
等等…… 我的代码究竟有什么问题?
如有任何帮助,我将不胜感激。提前致谢!
你要看这个逻辑,变化很小但效果很关键:
import os
pwd = os.getcwd()
playlist_links = ['A', 'B', 'C', 'D', 'E', 'F',]
for link in playlist_links:
path = os.path.join(pwd, link)
if not os.path.exists(path):
os.mkdir(path)
os.chdir(path)
with open(f"single-video-v5-{link}.txt", 'w', encoding="utf-8") as output_file:
output_file.write(link)
os.chdir(pwd)
执行后,查看当前工作目录的内容,然后是A/目录的内容:
$ ls
A B C D E F p.py
$ ls A/
single-video-v5-A.txt
可能会有帮助
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)
save_file = f'single-video-v5-{link}.txt' # create the file name
save_path = os.path.join(path,save_file) # create the save_path using above file name
# open the file write it and close
with open(save_path, 'w', encoding="utf8") as out_file:
out_file.write(link)
我认为没有必要调用 os.chdir()