未创建/写入文件

zhtb

这是我的代码:

org = "na"

OutputFile = open("F&FHOutput.txt", "a")

#Part 1
with open("input.txt") as file:
    for line in file:
        string,letter = line.strip().split(",")
        print(string + "," + letter + "," + string.replace(letter, ""))
        OutputFile.write(string + "," + letter + "," + string.replace(letter, ""))


#Part 2
def remove_strings_recursive(lines):
    if not lines:
        return ""

    word,letter = lines[0].rstrip().split(',')

    org = word

    word = word.replace(letter, '')

    print(org + "," + letter + "," + word)

    OutputFile.write(org + "," + letter + "," + word)
 
    return word + '\n' + remove_strings_recursive(lines[1:])

    

with open('input.txt', 'r') as file:
    lines = file.readlines()

    result = remove_strings_recursive(lines)


OutputFile.close()

我试图让它接受与打印相同的东西,并将它们放入程序创建的新文件中(如果该文件不存在)。每次我运行代码时,一切正常,但是找不到输出文件。有人可以帮忙吗?(对不起,代码混乱)

千瓦时

您的文件名带有特殊字符(&),这可能会引起问题。尝试将文件名更改为更标准的文件名。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章