Read a file with open command

John Machin sjmachin at lexicon.net
Fri Aug 11 06:39:50 EDT 2006


jean-jeanot wrote:
> I can access to a file with the command:
> file_obj = open ( " D:\My documents\Textfile.txt",'r')

With a space before the drive letter? I don't think so.
When asking questions, *don't* type what you thought you used,
copy/paste what you actually used.

>
> When I now try to read a file with the following command:
>
> file_obj = open ("D:\My documents\File.ods",'r') it doesn't function.
> The extension ods is coming from OpenOffice.org Calc.
>
> Why ?

You haven't told us what "it doesn't function" means, so we'll have to
play guessing games ...could be for at least two possible reasons:
(1) .ods files are binary and you didn't specify 'rb'
(2) you really typed "d:\my documents\file.ods" and the \f got
interpreted as a form-feed character. You should *never* type literal
Windows filenames like that. Instead, you have three choices:
(a) "d:\\my documents\\file.ods" # not recommended
(b) r"d:\my documents\file.ods"
(c) "d:/my documents/file.ods"

I'd suggest that you fix *both* of the above problems and try again.

HTH,
John




More information about the Python-list mailing list