python-3.x 我怎样才能在for循环中创建一个新文件

nkkqxpd9  于 2023-01-27  发布在  Python
关注(0)|答案(3)|浏览(141)
for name in names:
    with open(f"/Users/XYZ/Desktop/technology/z 100 days of code z/python/Day 24 Mail+Merge+Project+Start/Mail Merge Project Start/Output/ReadyToSend/new.txt") as file:
        final = letter
        final.replace("[name]", name)
        file.write(final)

尝试到创建一个新的文件为每一个名字在这列表的名字,但是它doesnot工作.得到这下面的错误.谢谢.
错误:[错误号22]无效参数:'/用户/XYZ/桌面/技术/z 100天的代码z/python/第24天邮件+合并+项目+开始/邮件合并项目开始/输出/准备发送/Aang\n. txt'
我期待这一个新的文件将被创建为每一个名称.我这样做是错误的或者是有一个不同的方式来实现我所期待的.
编辑:谢谢你的回复。最初我有一个以“/ReadyToSend/{name}.txt”结尾的f字符串,期望使用for循环中的名称创建一个新文件。
仍然得到一个错误.我已经添加了完整的代码.

with open("/Users/XYZ/Desktop/technology/z 100 days of code z/python/Day 24 Mail+Merge+Project+Start/Mail Merge Project Start/Input/Names/invited_names.txt") as invited_names_file:
    names = invited_names_file.readlines()

with open("/Users/XYZ/Desktop/technology/z 100 days of code z/python/Day 24 Mail+Merge+Project+Start/Mail Merge Project Start/Input/Letters/starting_letter.txt") as starting_letter_file:
    letter = starting_letter_file.read()

filename = "/Users/XYZ/Desktop/technology/z 100 days of code z/python/Day 24 Mail+Merge+Project+Start/Mail Merge Project Start/Output/ReadyToSend/new.txt"

for name in names:
    with open(file=filename.replace("new", name), mode="w") as file:
        final = letter
        final.replace("[name]", name)
        file.write(final)

最终:感谢所有的帮助!想通了,并得到了它的工作!但如果任何人有建议,清理这一切,请让我知道。最终代码如下。一个新的文件被创建为每个名称,每个文件的内容是针对该文件命名的人,正如所期望的。

with open("/Users/XYZ/Desktop/technology/z 100 days of code z/python/Day 24 Mail+Merge+Project+Start/Mail Merge Project Start/Input/Names/invited_names.txt") as invited_names_file:
    names = invited_names_file.read().split('\n')

with open("/Users/XYZ/Desktop/technology/z 100 days of code z/python/Day 24 Mail+Merge+Project+Start/Mail Merge Project Start/Input/Letters/starting_letter.txt") as starting_letter_file:
    letter = starting_letter_file.read().strip('\n')

filename = "/Users/XYZ/Desktop/technology/z 100 days of code z/python/Day 24 Mail+Merge+Project+Start/Mail Merge Project Start/Output/ReadyToSend/new.txt"

for name in names:
    with open(file=filename.replace("new", name), mode="w") as file:
        final = letter
        final = final.replace("[name]", name)
        file.write(final)
        print(final)

不完全确定strip()做了什么,但它确实起作用了,所以如果有人能澄清这一点,那将不胜感激!

ttp71kqs

ttp71kqs1#

您需要在创建文件之前替换文件名:

filename = f"/Users/XYZ/Desktop/technology/z 100 days of code z/python/Day 24 Mail+Merge+Project+Start/Mail Merge Project Start/Output/ReadyToSend/new.txt"
for name in names:
    with open(filename.replace("new", name), "w") as file:
        file.write("test")
p3rjfoxz

p3rjfoxz2#

1.确保文件路径有效。(如果在Windows上,则包括C:\)
1.指定要在其中打开文件的mode,您将以读取模式打开它。
1.您不是在创建新文件,而是在打开同一个文件,需要指定不同的路径。
https://docs.python.org/3/library/functions.html#open

km0tfn4u

km0tfn4u3#

删除文件名中的“\n”

with open("/Users/XYZ/Desktop/technology/z 100 days of code z/python/Day 24 Mail+Merge+Project+Start/Mail Merge Project Start/Input/Names/invited_names.txt") as invited_names_file:
  names = invited_names_file.read().split('\n')

with open("/Users/XYZ/Desktop/technology/z 100 days of code z/python/Day 24 Mail+Merge+Project+Start/Mail Merge Project Start/Input/Letters/starting_letter.txt") as starting_letter_file:
    letter = starting_letter_file.read().strip('\n')

相关问题