By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,570 Members | 970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,570 IT Pros & Developers. It's quick & easy.

Read a file with open command

P: n/a
I can access to a file with the command:
file_obj = open ( " D:\My documents\Textfile.txt",'r')

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 ?

jean-jeanot

Aug 11 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Hi,
simply use file_obj = open ("D:\My documents\File.ods",'rb') for
opening file in binary access mode, which is required for binary files
on MS Windows.
Honza

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

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 ?

jean-jeanot
Aug 11 '06 #2

P: n/a
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

Aug 11 '06 #3

P: n/a
Sorry, but the access mode is not binary with XP Windows. Finally for
reading the file it is necessary to use a slash or a double backslash.
If the file is a simple textfile, using a backslash is perhaps not
recommended but it is functionning.
Anyway many thanks.Here is the program:
>>file_obj= open ("D:/Mes documents/ADB Anna.ods",'r')
s = file_obj
s.readlines()
Jean-Jeanot

Jan Svec a écrit :
Hi,
simply use file_obj = open ("D:\My documents\File.ods",'rb') for
opening file in binary access mode, which is required for binary files
on MS Windows.
Honza

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

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 ?

jean-jeanot
Aug 11 '06 #4

P: n/a
On 11 Aug 2006 09:39:23 -0700, jean-jeanot <je********@neuf.frwrote:
Anyway many thanks.Here is the program:
>file_obj= open ("D:/Mes documents/ADB Anna.ods",'r')
s = file_obj
s.readlines()
Please remember not to top-post :)

Try this
>>s = open ("D:/Mes documents/ADB Anna.ods",'r')
s.readlines()
s.close()
or
>>s = open ("D:/Mes documents/ADB Anna.ods",'r').readlines()
HTH :)
Aug 11 '06 #5

P: n/a
Dear Sybrel,

I am delighted to know that you have been enlighted through my
question.
I am aware of my stupidity and I would say of my ignorance.If all
Python users were not ignorant I suppose the Python group would be
superfluous. I would suggest that if if you think that a question is
supid please do not answer it.In French we say: "There is no stupid
question but answers can be stupid". For the citation of Zappa I am
convinced that when Zappa is speaking of world stupidity he is thinking
to stupidity and wickedness of mankind and not to ignorance.
Anyway the open command with a file having the extension ods must be
used with a slash. But with a double backslash it is functionning too.
In every documentation I posess on Python it is mentioned that an
absolute path in Windows starts with a backslash.Python accepts
platform-specific syntax ( See Python by C. Fehilly , p 306, Python in
a nutshell by Martelli, etc,etc)
Anyway thank you for your answer.

jean-Jeanot

Sybren Stuvel a écrit :
jean-jeanot enlightened us with:
I can access to a file with the command:
file_obj = open ( " D:\My documents\Textfile.txt",'r')

Which is the wrong way. Use forward slashes, escape your backslashes,
or use raw strings.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Aug 11 '06 #6

P: n/a
In <11*********************@75g2000cwc.googlegroups.c om>, jean-jeanot
wrote:
Sorry, but the access mode is not binary with XP Windows.
But ods files are binary so you better open them as binary files. Windows
does a "magical" translation of line ending bytes and stops processing a
file if it hits a \x26 byte if the file was opened in the default text
mode.
Finally for reading the file it is necessary to use a slash or a double
backslash. If the file is a simple textfile, using a backslash is
perhaps not recommended but it is functionning.
The slashes don't have anything to do with the content of the file. For
both, binary and text files, you have to escape backslashes in literal
strings or sooner or later you will be bitten by a backslash + character
combination that's a valid escape sequence in literal strings.
>file_obj= open ("D:/Mes documents/ADB Anna.ods",'r')
s = file_obj
s.readlines()
This doesn't make much sense as `ods` files don't contain text lines but
are binary. A ZIP archive containing XML files to be more precise.

If you want to read the raw binary data into memory it's better to use::

file_obj = open('D:/Mes documents/ADB Anna.ods', 'rb')
data = file_obj.read()
file_obj.close()

Ciao,
Marc 'BlackJack' Rintsch
Aug 11 '06 #7

P: n/a
jean-jeanot wrote:
Dear Sybrel,

I am delighted to know that you have been enlighted through my
question.
I am aware of my stupidity and I would say of my ignorance.If all
Python users were not ignorant I suppose the Python group would be
superfluous. I would suggest that if if you think that a question is
supid please do not answer it.In French we say: "There is no stupid
question but answers can be stupid". For the citation of Zappa I am
convinced that when Zappa is speaking of world stupidity he is thinking
to stupidity and wickedness of mankind and not to ignorance.
Ummm, he did not say that your question was stupid. The Zappa quote is
included as part of what we refer to as a 'signature'. In the case of
Sybren Stuvel's posts to this newsgroup, the Frank Zappa quote is included
in every message that he posts. Not just messages that he posts to you.

Secondly, I notice that when you quoted Sybren's message in your reply, your
newsreader attributed the quoted text with: "Sybren Stuvel a écrit :"
Likewise, when Sybren replied to your message, his newsreader attributed
the quoted text with, "jean-jeanot enlightened us with:"

Do you see what I mean? You didn't write "Sybren Stuvel a écrit" because
Sybren was french, did you? Of course, not! Your mail/news application
included that attribution by default. By the same token, Sybren's
mail/news application defaulted to this attribution: 'jean-jeanot
enlightened us with:"

When reading people's responses to your queries, stick to the material that
they actually "write" at the time of the response. Ignore the stuff that
they entered while configuring their respective news/mail reader. Little
items like message signatures and quote attributions are, at best,
reflective of the personality and/or philosophy of the author, and say
nothing of the person receiving the message. :-)

Aug 11 '06 #8

P: n/a

Sorry, I regret my reaction.

Jean-jeanot

AlbaClause a écrit :
jean-jeanot wrote:
Dear Sybrel,

I am delighted to know that you have been enlighted through my
question.
I am aware of my stupidity and I would say of my ignorance.If all
Python users were not ignorant I suppose the Python group would be
superfluous. I would suggest that if if you think that a question is
supid please do not answer it.In French we say: "There is no stupid
question but answers can be stupid". For the citation of Zappa I am
convinced that when Zappa is speaking of world stupidity he is thinking
to stupidity and wickedness of mankind and not to ignorance.

Ummm, he did not say that your question was stupid. The Zappa quote is
included as part of what we refer to as a 'signature'. In the case of
Sybren Stuvel's posts to this newsgroup, the Frank Zappa quote is included
in every message that he posts. Not just messages that he posts to you.

Secondly, I notice that when you quoted Sybren's message in your reply, your
newsreader attributed the quoted text with: "Sybren Stuvel a écrit :"
Likewise, when Sybren replied to your message, his newsreader attributed
the quoted text with, "jean-jeanot enlightened us with:"

Do you see what I mean? You didn't write "Sybren Stuvel a écrit" because
Sybren was french, did you? Of course, not! Your mail/news application
included that attribution by default. By the same token, Sybren's
mail/news application defaulted to this attribution: 'jean-jeanot
enlightened us with:"

When reading people's responses to your queries, stick to the material that
they actually "write" at the time of the response. Ignore the stuff that
they entered while configuring their respective news/mail reader. Little
items like message signatures and quote attributions are, at best,
reflective of the personality and/or philosophy of the author, and say
nothing of the person receiving the message. :-)
Aug 11 '06 #9

P: n/a
AlbaClause wrote:
jean-jeanot wrote:
<snip>
Ummm, he did not say that your question was stupid. The Zappa quote is
included as part of what we refer to as a 'signature'. In the case of
Sybren Stuvel's posts to this newsgroup, the Frank Zappa quote is included
in every message that he posts. Not just messages that he posts to you.
First up, I *like* the Frank Zappa signature quote; it has a George
Carlin feel to it and takes a swipe at our overly-protective cultures
[side note: I bought an electric heating pad last night that came with
instructions not to use it in the bathtub! Well, duh!]. But, can you
see where the signature on a response to a tutor list -- where people
are already feeling a bit intimidated and/or inadequate and therefore
may feel a wee bit "stupid" -- might be taken the wrong way?

I have spent many years teaching mathematics and physics in a classroom
setting and have come to realize that, as a teacher, just about
anything I say/do can be blown way out of proportion. So I don't use
sarcasm or "fun" little put-downs and I treat every question as if it
is the most important matter because, to the student, it is. Do I get
tired of answering the same thing over and over? Yes!! Many times I
will ask if the student has read the textbook and, if not, I will
request they give it a try (much in the same way we ask if they have
read any tutorials), but I take Homeric efforts not to offend them and,
to that end, modify my behavior in order to teach them mathematics.

My point is that this is a wonderful service you tutors provide, but
the Zappa signature may not be the best choice for this setting. Most
people will read it and get a laugh (as did I), but how many have taken
it the way jean-jeanot did and walk away feeling insulted? How many
will not post a response expressing their feelings, never post a
question again or, worst case, decide Python is not for them?

Again, I admire this list and those of you you maintain it. These are
just my thoughts. YMMV.

--greg

Aug 12 '06 #10

P: n/a
gs*********@gmail.com wrote:
AlbaClause wrote:
>jean-jeanot wrote:
<snip>
>Ummm, he did not say that your question was stupid. The Zappa quote is
included as part of what we refer to as a 'signature'. In the case of
Sybren Stuvel's posts to this newsgroup, the Frank Zappa quote is
included
in every message that he posts. Not just messages that he posts to you.

First up, I *like* the Frank Zappa signature quote; it has a George
Carlin feel to it and takes a swipe at our overly-protective cultures
[side note: I bought an electric heating pad last night that came with
instructions not to use it in the bathtub! Well, duh!]. But, can you
see where the signature on a response to a tutor list -- where people
are already feeling a bit intimidated and/or inadequate and therefore
may feel a wee bit "stupid" -- might be taken the wrong way?

I have spent many years teaching mathematics and physics in a classroom
setting and have come to realize that, as a teacher, just about
anything I say/do can be blown way out of proportion. So I don't use
sarcasm or "fun" little put-downs and I treat every question as if it
is the most important matter because, to the student, it is. Do I get
tired of answering the same thing over and over? Yes!! Many times I
will ask if the student has read the textbook and, if not, I will
request they give it a try (much in the same way we ask if they have
read any tutorials), but I take Homeric efforts not to offend them and,
to that end, modify my behavior in order to teach them mathematics.

My point is that this is a wonderful service you tutors provide, but
the Zappa signature may not be the best choice for this setting. Most
people will read it and get a laugh (as did I), but how many have taken
it the way jean-jeanot did and walk away feeling insulted? How many
will not post a response expressing their feelings, never post a
question again or, worst case, decide Python is not for them?

Again, I admire this list and those of you you maintain it. These are
just my thoughts. YMMV.

--greg
This is getting a little off-topic, but my feeling is that if you're unable
to discern the difference between the substance of the message, and the
"personal style" of the author (eg: quote attributions and/or signature
lines) then you probably shouldn't be attempting to code in Python or any
other structured language.

A newsgroup post is quite structured -- just as a Python script is -- there
is the header, which contains the subject, the sender, the recipient, and
other protocol information; and the text body. The text body is also quite
structured. The text body can contain a quote from a prior message, the
added comments that make up the substance of the message, and a
signature/tag line. If a person has great difficulty in differentiating
the various parts of a newsgroup message, then perhaps structured
programming languages are not for them.

Perhaps we should begin a new message passing convention. One where the
substance of the post is contained within braces -- like C/C++ code? Just
kidding! LOL

--
--
There are several things that I will never be:
* I will never be attracted to females.
* I will never enjoy the company of others.
Exactly how these realities bode for my enemy, is not of my concern.

Aug 12 '06 #11

P: n/a
gs*********@gmail.com wrote:
[snip]
My point is that this is a wonderful service you tutors provide, but
the Zappa signature may not be the best choice for this setting.
This is *not* the tutor list.
>Most people will read it and get a laugh (as did I), but how many have taken
it the way jean-jeanot did and walk away feeling insulted? How many
will not post a response expressing their feelings, never post a
question again or, worst case, decide Python is not for them?
<humour>
Possibly one, were he still alive: St Aloysius "of whom it is said in
the book of the monk Eustachius that when he heard a man breaking wind
with deafening noise he immediately burst into tears and could only be
consoled by prayers" [from "The Good Soldier Švejk ..." by Jaroslav
Hašek [tr. Parrott (the Sir Cecil variety, not the Norwegian Blue)]]
</humour>

Cheers,
John

Aug 13 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.