Ho il seguente codice Python per scrivere i file di dipendenza di un progetto. Funziona bene con Python 2.x, ma durante il test con Python 3 segnala un errore.Operatore Python 3 >> per stampare su file
depend = None
if not nmake:
depend = open(".depend", "a")
dependmak = open(".depend.mak", "a")
depend = open(".depend", "a")
print >>depend, s,
Ecco l'errore:
Traceback (most recent call last):
File "../../../../config/makedepend.py", line 121, in <module>
print >>depend, s,
TypeError: unsupported operand type(s) for >>:
'builtin_function_or_method' and '_io.TextIOWrapper'
Qual è il modo migliore per ottenere questo lavoro con Python 2.xe 3.x?
"con python2.x e 3.x"? Non puoi avere lo stesso codice in entrambe le versioni. Di solito, si usa lo strumento '2to3' per convertire Python 2 funzionante in Python 3. –
Puoi usare' da __future__ import print_function' per ottenere la funzione python 3 print() in Python 2. Vedi anche http://python3porting.com –