'User defined writefile function won't save output to txt file
I've created two functions to read and write text files as a larger part of a caeser cipher program where I need to encode and decode content from and to different text files. These are:
def readfile(f):
try:
file_object = open(f, 'r+')
message = file_object.read()
file_object.close()
return message
except:
print("No such file or dictionary!")
quit()
and
def writefile(message):
try:
f = open("file", 'a')
f.write(message + '\n')
f.close()
except ValueError:
print("No such file or dictionary!")
quit()
except:
print("Input must be a string!")
quit()
The problem seems to be that my write function won't actually save the program's output to the next text file. I've been stumped for sometime, could use a hand.
EDIT Thanks Barmar! my write function was writing to a set file that didn't exist. This worked for me:
def writefile(message):
try:
f = open(input("Please enter a file for writing:"), 'w')
f.write(message + '\n')
f.close()
except ValueError:
print("The selected file cannot be open for writing!")
quit()
except:
quit()
Solution 1:[1]
Thanks Barmar! my write function was writing to a set file that didn't exist. This worked for me:
def writefile(message):
try:
f = open(input("Please enter a file for writing:"), 'w')
f.write(message + '\n')
f.close()
except ValueError:
print("The selected file cannot be open for writing!")
quit()
except:
quit()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Darth Kenobi66 |
