[Python] Output su file di testo

Pubblicità

mariolino1988

Bannato a Vita
Messaggi
172
Reazioni
1
Punteggio
38
salve amici spero che qui mi dareste un aiuto ho fatto questo codice vorrei che il contenuto del codice si vedrebbe in un fil txt grazie
Python:
import socket
                                         
import shutil 
 
hostname=socket.gethostname()
IPAddr=socket.gethostbyname(hostname)
print("Your Computer Name is:"+hostname)
print("Your Computer IP Address:"+IPAddr)
shutil.copy ('mariolino.py', 'test.txt')
 
Ultima modifica da un moderatore:
Posto che non ho ben capito perché copiare il contenuto di un file .py in un file .txt, visto che ce l’hai sul file .py stesso.
Comunque ciò che vuoi fare tu si potrebbe fare senza librerie.

Python:
import socket
import sys

hostname=socket.gethostname()
IPAddr=socket.gethostbyname(hostname)
print("Your Computer Name is:"+hostname)
print("Your Computer IP Address:"+IPAddr)

with open(sys.argv[0], 'r') as file:
    file_content = file.read()

with open('newfile.txt', 'w') as file:
    file.write(file_content)

Se invece tu vuoi solamente copiare le informazioni che ti compaiono in console invece di tutto il file:

Python:
import socket

hostname=socket.gethostname()
IPAddr=socket.gethostbyname(hostname)
print("Your Computer Name is:"+hostname)
print("Your Computer IP Address:"+IPAddr)

with open('newfile.txt', 'w') as file:
    file.write('Your Computer Name is:', hostname)
    file.write('Your Computer IP Address:', IPAddr)
 
Traceback (most recent call last):
File "C:/Users/mario/mariolino.py", line 10, in <module>
file.write('Your Computer Name is:', hostname)
TypeError: TextIOWrapper.write() takes exactly one argument (2 given) mi da questo errrore grazie
 
Si scusa, un lapsus momentaneo. Sostituisci con queste due righe di codice sia il primo che il secondo write
Python:
file.write('Your Computer Hostname is: ' + hostname)
file.write('Your IP Address: ' + IPAddr)
 
with open('newfile.txt', 'w') as file:
file.write('Your Computer Hostname is: ' + hostname)
file.write('Your IP Address: ' + IPAddr) sulla prima riga mi da errore f
 
non riesco a scrivertelo perche sulla prima riga mi da l errore ora ti scrivo cosa die
expected an intend block after 'with' statement on line 9
 
Come detto sopra riportami tutto il codice. Se proprio non riesci a copiarlo e incollarlo va bene anche una foto, perché così proprio non riesco ad aiutarti
 
Python:
import socket

hostname=socket.gethostname()
IPAddr=socket.gethostbyname(hostname)
print("Your Computer Name is:"+hostname)
print("Your Computer IP Address:"+IPAddr)

with open('newfile.txt', 'w') as file:
  
file.write('Your Computer Hostname is: ' + hostname)
file.write('Your IP Address: ' + IPAddr)
ECCOLO PLS AIUTAMI
 

Allegati

  • Screenshot (12).webp
    Screenshot (12).webp
    54.2 KB · Visualizzazioni: 28
Ultima modifica da un moderatore:
Se badi in python non si usano le parentesi graffe per racchiudere le parti di codice quindi è imperativo indentare correttamente il codice altrimenti si perde la relazione questa riga è un sottocomando di quella prima.
 
Pubblicità
Pubblicità
Indietro
Top