Read a file with open command

J

jean-jeanot

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
 
J

Jan Svec

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
 
S

Sybren Stuvel

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
 
J

John Machin

jean-jeanot said:
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
 
J

jean-jeanot

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:

Jean-Jeanot

Jan Svec a écrit :
 
J

jean-jeanot

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 :
 
M

Marc 'BlackJack' Rintsch

jean-jeanot said:
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.

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
 
A

AlbaClause

jean-jeanot said:
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. :)
 
G

gslindstrom

AlbaClause said:
jean-jeanot wrote:
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
 
A

AlbaClause

AlbaClause said:
jean-jeanot wrote:
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

--
 
J

John Machin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top