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

script fichiers binaires lecture écriture

P: n/a
Je connais mal python et n'est pas trop le temps de m'y plonger bien
que cela semble être assez puissant...

import sys
import ixio
import os

M = ixio.getMAC("eth0")
S = "%08X %08X" % (M[0] | M[1]<<8 | M[2]<<16 | M[3]<<24, M[4] |
M[5]<<8)
K = "Errorin:"
if len(sys.argv) <> 3:
print "Usage %s <src-file> <dst-file>" % sys.argv[0]
else:
I = open(sys.argv[1],"rb")#ouverture de tpsd.pre avec le flag rb
pour read in binary mode
O = open(sys.argv[2],"wb")
while 1:
blk = I.read(1<<13)
try:
i = blk.index(K)
blk = "%sErrorin:%s%s" \
% (blk[:i],S,blk[i+len(K)+len(S):])
O.write(blk)
except ValueError:
O.write(blk)
if len(blk)<1<<13:
break
O.close()
I.close()

Voici l'erreur que j'obtiens en essayant d'exécuter ce script:

AttributeError: 'string' object has no attribute 'index'

D'après moi, index est une méthode de la classe string et non un
attribut...Je ne comprend donc pas... Enfin, je précise que je suis
malheureusement en version 1.5 de python... :(

Autre question: 1<<13 est censé être une taille en octet: comment
cela se lit-t-il?

Merci d'avance de m'aclairer sur cette erreur et cette question.

Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In English:

I don't know much about python and I won't have much time to learn much
about it even if it seems powerful...

import sys
import ixio
import os

M = ixio.getMAC("eth0")
S = "%08X %08X" % (M[0] | M[1]<<8 | M[2]<<16 | M[3]<<24, M[4] |
M[5]<<8)
K = "Errorin:"
if len(sys.argv) <> 3:
print "Usage %s <src-file> <dst-file>" % sys.argv[0]
else:
I = open(sys.argv[1],"rb")#ouverture de tpsd.pre avec le flag rb
pour read in binary mode
O = open(sys.argv[2],"wb")
while 1:
blk = I.read(1<<13)
try:
i = blk.index(K)
blk = "%sErrorin:%s%s" \
% (blk[:i],S,blk[i+len(K)+len(S):])
O.write(blk)
except ValueError:
O.write(blk)
if len(blk)<1<<13:
break
O.close()
I.close()

Here is an error I get trying to run this script:

AttributeError: 'string' object has no attribute 'index'

According to me, index() is a method of the string class but not an
attribute. So I don't understand the error message. Be aware that I'm
using pyhton 1.5, unfortunately...

Another question: "1<<13" is supposed to be a size in bytes but can you
explain to me how to read this?

Thanks for helping me understanding the error message and size thing!!

Jul 19 '05 #2

P: n/a
Statesman wrote:
In English:

I don't know much about python and I won't have much time to learn much
about it even if it seems powerful...
(snip code)
Here is an error I get trying to run this script:

AttributeError: 'string' object has no attribute 'index'

According to me, index() is a method of the string class but not an
attribute. So I don't understand the error message.
In Python, functions are first class citizens (a function is an object
you can bind to a variable, pass as an argument, return from another
function etc...), so methods *are* actually attributes...
Be aware that I'm
using pyhton 1.5, Err... latest is 2.4.1, and the language has really, really changed. You
should consider upgrading...
unfortunately...


BTW, in 1.5.x, you can use the String module instead of string class
methods:

import String
s = "allo"
String.index(s, "a")

but really, consider upgrading to a newer version...

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 19 '05 #3

P: n/a
Statesman wrote in comp.lang.python:

<en>
Hi Statesman
comp.lang.python is the english-speaking Python forum. You may want to
try the french-speaking one at fr.comp.lang.python (xpost and fu2 set)
</en>
Je connais mal python et n'est pas trop le temps de m'y plonger bien
que cela semble être assez puissant...

import sys
import ixio
import os

M = ixio.getMAC("eth0")
S = "%08X %08X" % (M[0] | M[1]<<8 | M[2]<<16 | M[3]<<24, M[4] |
M[5]<<8)
K = "Errorin:"
if len(sys.argv) <> 3:
print "Usage %s <src-file> <dst-file>" % sys.argv[0]
else:
I = open(sys.argv[1],"rb")#ouverture de tpsd.pre avec le flag rb
pour read in binary mode
O = open(sys.argv[2],"wb")
while 1:
blk = I.read(1<<13)
try:
i = blk.index(K)
blk = "%sErrorin:%s%s" \
% (blk[:i],S,blk[i+len(K)+len(S):])
O.write(blk)
except ValueError:
O.write(blk)
if len(blk)<1<<13:
break
O.close()
I.close()

Voici l'erreur que j'obtiens en essayant d'exécuter ce script:

AttributeError: 'string' object has no attribute 'index'

D'après moi, index est une méthode de la classe string et non un
attribut...Je ne comprend donc pas...
En Python, les fonctions sont des objets comme les autres. Donc les
methodes sont effectivement des attributs... (bon, dans le détail c'est
un poil plus compliqué que ça, mais là je te laisse consulter la doc...
si tu tiens vraiment à comprendre tous les détails d'implémentation)
Enfin, je précise que je suis
malheureusement en version 1.5 de python... :(
Euh... la dernière est la 2.4.1, il serait peut-être temps d'envisager
une mise à jour.

En attendant, dans la 1.5.x, tu peux utiliser le module String:
import String
s = "allo"
String.index(s, "a")

(de mémoire, pas testé).

Autre question: 1<<13 est censé être une taille en octet: comment
cela se lit-t-il?
opérateur de décalage de bits... 1 << 13 == 8192
Merci d'avance de m'aclairer sur cette erreur et cette question.


HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 19 '05 #4

P: n/a
bruno modulix <on***@xiludom.gro> writes:
Be aware that I'm
using pyhton 1.5,

Err... latest is 2.4.1, and the language has really, really changed. You
should consider upgrading...
unfortunately...


BTW, in 1.5.x, you can use the String module instead of string class
methods:


In python 1.5.x, string objects didn't have methods. That's why he's
getting the error. He has to use the string module instead.

I agree with bruno - you *really* should upgrade.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.