Je plante python toutes les 3 lignes

Résolu
cs_steede Messages postés 13 Date d'inscription vendredi 31 décembre 2004 Statut Membre Dernière intervention 26 mars 2005 - 17 févr. 2005 à 23:13
taz_iup Messages postés 179 Date d'inscription mercredi 13 octobre 2004 Statut Membre Dernière intervention 21 mai 2014 - 22 févr. 2005 à 11:46
bonjour,


Sauriez vous me dire pourquoi le fichier suivant à l'execution :


from os import *


def openfich():
filename = raw_input('Enter file name: ')
file = open(filename, 'r')
allLines = file.readlines()


retourne l'erreur :


Traceback (most recent call last):
File "", line 1, in -toplevel-
openfich()
File "C:\Python24\_M300\chsys.pyw", line 5, in openfich
file = open(filename, 'r')
TypeError: an integer is required


le code est tiré d'un exemple...


Merci.

4 réponses

taz_iup Messages postés 179 Date d'inscription mercredi 13 octobre 2004 Statut Membre Dernière intervention 21 mai 2014 3
22 févr. 2005 à 11:46
Encore une petite précision

j'avais oublie de copier la petite note...



Note: This function is intended for low-level I/O. For normal
usage, use the built-in function <tt class= "function">open()</tt>, which returns a
``file object'' with <tt class="method">read()</tt> and <tt class="method">write()</tt> methods (and many more).



Le truc c'est qu'étant donnée la manière don est ilmporte os, os.open écrase la fonction open built-in donc la doc est :



* : <nobr><tt class =\\"function\\" id= \\"l2h-52\\" xml:id=\\"l2h-52\\">open</tt>(</nobr>,
<var>filename</var>[<var>, mode</var>[<var>,
bufsize</var>]<var></var>]<var></var>)

An alias for the <tt class="function">file()</tt> function above.

la methode file dont te parlait le jeune homme avant moi. ( toujours extrait de la doc en ligne. )



* : <nobr><tt class =\\"function\\" id=\\"l2h-25\\" xml:id=\\"l2h-25\\">file</tt>(</nobr>,
<var>filename</var>[<var>, mode</var>[<var>,
bufsize</var>]<var></var>]<var></var>)

Return a new file object (described in section 2.3.9, ``File Objects''). The first two arguments are
the same as for
stdio
's <tt class=\"cfunction\">fopen()</tt>:
<var>filename</var> is the file name to be opened, <var>mode</var> indicates how
the file is to be opened:
'r'
for reading,
'w'
for
writing (truncating an existing file), and
'a'
opens it for
appending (which on some Unix systems means
that all writes append to the end of the file, regardless of the
current seek position).
Modes
'r+'
,
'w+'
and
'a+'
open the
file for updating (note that
'w+'
truncates the file). Append
'b'
to the mode to open the file in binary mode, on systems that
differentiate between binary and text files (else it is ignored). If the file
cannot be opened, <tt class=\"exception\">IOError</tt> is raised.

In addition to the standard <tt class=\"cfunction\">fopen()</tt> values
<var>mode</var> may be
'U'
or
'rU'
. If Python is built
with universal newline support (the default) the file is opened as a text file,
but lines may be terminated by any of
'\n'
, the Unix end-of-line
convention,
'\r'
, the Macintosh convention or
'\r\n'
,
the Windows convention. All of these external representations are seen as
'\n'
by the Python program. If Python is built without universal
newline support <var>mode</var>
'U'
is the same as normal text
mode. Note that file objects so opened also have an attribute called <tt class=\"member\">newlines</tt> which has a value of
None
(if no
newlines have yet been seen),
'\n'
,
'\r'
,
'\r\n'
, or a tuple containing all the newline types seen.

If <var>mode</var> is omitted, it defaults to
'r'
. When opening
a binary file, you should append
'b'
to the <var>mode</var> value
for improved portability. (It's useful even on systems which don't treat binary
and text files differently, where it serves as documentation.) The optional <var>bufsize</var> argument specifies the
file's desired buffer size: 0 means unbuffered, 1 means line buffered, any other
positive value means use a buffer of (approximately) that size. A negative
<var>bufsize</var> means to use the system default, which is usually line
buffered for tty devices and fully buffered for other files. If omitted, the
system default is used.

The <tt class=\"function\">file()</tt> constructor is new in Python 2.2. The
previous spelling, <tt class=\"function\">open()</tt>, is retained for
compatibility, and is an alias for <tt class=\"function\">file()</tt>

A mon avis, etant donné qu'il utilise la methode readlines, dans ton
exemple il voulait plutot utilise la fonction built-in alors change ton
import en import os et rajoute os. devant chaque fonction qui provienne
de ton module. ( en effet il n'y a pas de meth readlines sur un
descripteur de fichier, c'est un entier :) )

Bon voila en esperant t'avoir eclairer...

@ +
3
cs_rodrigos Messages postés 84 Date d'inscription jeudi 24 janvier 2002 Statut Membre Dernière intervention 4 avril 2006 2
18 févr. 2005 à 08:50
salut,

au lieu de la commande open tu peux essayer la commande file(filename, 'r')

en plus file est une méthode de python donc à eviter comme nom de variable:






fichier = file(filename, 'r')
0
cs_steede Messages postés 13 Date d'inscription vendredi 31 décembre 2004 Statut Membre Dernière intervention 26 mars 2005
18 févr. 2005 à 13:11
J'avais déjà remarqué que file est une méthode de python, mais bon...

Par contre aujourd'hui le prog. marche sans problème (sans modif !!!).

Je soupçonne mon firewall de ne pas tout laisser passer et de squeezer qqe octets.

C'est AVAST antivirus. Avez vous ce genre de probléme ?

Merci.
0
taz_iup Messages postés 179 Date d'inscription mercredi 13 octobre 2004 Statut Membre Dernière intervention 21 mai 2014 3
22 févr. 2005 à 11:38
Voici la réponse a ta question :



Extrait de la doc en ligne de python 2.4 ( www.python.org ) :

* : <nobr><tt class=\\"function\\" id=\\"l2h-1548\\" xml:id=\\"l2h-1548\\">open</tt>(</nobr>,
<var>file, flags</var>[<var>,
mode</var>]<var></var>)

Open the file <var>file</var> and set various flags according to
<var>flags</var> and possibly its mode according to <var>mode</var>. The default
<var>mode</var> is
0777
(octal), and the current umask value is
first masked out. Return the file descriptor for the newly opened file.
Availability: Macintosh, Unix, Windows.
For a description of the flag and mode values, see the C run-time
documentation; flag constants (like <tt class=\"constant\">O_RDONLY</tt> and <tt class=\"constant\">O_WRONLY</tt>) are defined in this module too (see below).

en gros open prend un nom de fichier, un flag( un entier ) et facultativement un mode ( le 'r' )

Il manque donc le parametre flag.



Puis pour reprendre ce qui a été dit cee n'est pas tres malin
d'utiliser comme nom de variable des fonction et nom de classes
built-in. si tu es comme moi un peu radin sur le nombre de caractere
pour les nom de vraible tu n'a qu'a l'appeler fd ou df ( si tu prefere
en francais ) pour File Descriptor ( ou Descripteur de Fichier en
francais. ) comme ca tu n'auras plus de problème de nommage. :D

Voila A plus.
0
Rejoignez-nous