I’m writing a program in Python where you can write to a text file from the program, remotely. Relevant Code:
namein = input("What do you want the filename to be? Don't include an extension...\n")
extin = input("What would you like the extension to be? This program supports:\nMicrosoft Word Document: '.docx'\nPlain Text File: '.txt'\nRich Text File: '.rtf'\nPages Document: '.pages'\n")
aw = input("Do you want to append your text file, or rewrite the whole thing? (append/write) ")
if aw == 'append':
textin = input("In the line below, write the text you want to put into your text document!\n\n")
outfile = open(namein + extin, 'a')
outfile.write(textin)
outfile.flush()
print("Great! Now your text file has been updated!")
print("Your text file:\n")
outfile.close()
outfile = open(namein + extin, 'r')
print(outfile.read())
When someone chooses a non ‘.txt’ file, one can’t open the file! It just says error, the file can’t be opened. Is there a way around this?
Your code is bass ackwards.
You should be asking them what sort of file they want to save the text as and set the extension based on that.
Them have to put the dot in isn’t good either…
After that while you’ll get away with saving straight text, with an rtf extension or doc. DocX and pdf are not going to work. you’d need to create document of that type and then add the text as content.