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 + '\n' + 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)
Your file name has a special character ( &
), which can cause problems. Try changing the file name to a more standard one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.