3

I created a file using the write command, and for some reason when I try to open it with my text editor after, it comes up with this message: "The document python_file.rtf could not be opened"

Here's exactly what I did:

infile=open("python_file.rtf","w")
infile.write("insert string here")
infile.close()

Then when I try to open the file (I can find it in documents and everything) it gives me that error message. Can anyone tell me why? I am very new to programming.

1
  • 1
    Question is no longer on hold. You can accept one of the answers now. Commented Aug 5, 2013 at 19:27

4 Answers 4

6

That's because rtf isn't just plain text format. Save it as python_file.txt for example or create file that compatibile with rtf format, for example:

>>> infile=open("python_file.rtf","w")
>>> infile.write("{\r test \par }")
>>> infile.close()
Sign up to request clarification or add additional context in comments.

Comments

4

RTF files are not text files. You wrote a text file, but called it ".rtf", so your operating system is trying to treat it as an RTF, and failing because the contents don't match the RTF file format.

Change it to "python_file.txt" and I bet it'll work fine.

2 Comments

"RTF files are not text files" That's not true in the sense that you can open an RTF file in "text mode" in a python program. Of course, as you mention the issue is about the format of the text in the RTF file that differs from a plain ol' TXT file.
A minor semantic ambiguity, but I see your point. In the strict Unix sense, it's a text file. But it's not a ".txt" text file.
2

You try to save this to a RichTextFormated File, but "insert string here" is only a character sequence. Try to save it as python_file.txt and open it with a notepad application, then you should see the text.

If you want to save RTF files you should check how this files are internaly formated. For your example this would be:

infile=open("python_file.rtf","w")
infile.write("{\rtf1 insert string here }")
infile.close()

Comments

0

Try using a different file extension (.txt instead of .rtf). Maybe your OS is trying to open it as a formatted document (i.e. MS Word).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.