Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

python - File not being created/written in

This is my code:

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 + '
' + remove_strings_recursive(lines[1:])

    

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

    result = remove_strings_recursive(lines)


OutputFile.close()

I am trying to have it take the same things that are being printed and put them into a new file that the program creates if the file doesn't exist. Every time I run the code, everything works fine but the output file is nowhere to be found. Could someone please help? (Sorry about the messy code)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your file name has a special character (&), which can cause problems. Try changing the file name to a more standard one.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...