Pcarlitz
Pcarlitz

Reputation: 223

Python file cannot be opened

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.

Upvotes: 3

Views: 3120

Answers (4)

vector9x
vector9x

Reputation: 66

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).

Upvotes: 0

tbraun89
tbraun89

Reputation: 2234

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()

Upvotes: 2

Grant Birchmeier
Grant Birchmeier

Reputation: 18484

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.

Upvotes: 4

solusipse
solusipse

Reputation: 587

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()

Upvotes: 6

Related Questions