[Risolto]Catturare output comando python in variabile python

Linguaggi di programmazione: php, perl, python, C, bash e tutti gli altri.
Avatar utente
omegaub
Scoppiettante Seguace
Scoppiettante Seguace
Messaggi: 342
Iscrizione: martedì 2 dicembre 2008, 12:50
Desktop: gnome
Distribuzione: Ubuntu 18.04.4 LTS
Sesso: Femminile
Località: Boschi romani

[Risolto]Catturare output comando python in variabile python

Messaggio da omegaub »

Ciao a tutti!
Ho "googlato" tanto na non ho trovato risposta: sto scrivendo uno script in python ed in una variabile vorrei salvare l'output del comando che do su shell python help('modules'), non riesco proprio a trovare una soluzione, qualcuno può aiutarmi?
Geazie! :)
Ultima modifica di trekfan1 il giovedì 9 agosto 2018, 17:12, modificato 2 volte in totale.
Motivazione: Non serve il tutto maiuscolo per il [Risolto], grazie
Avatar utente
nuzzopippo
Entusiasta Emergente
Entusiasta Emergente
Messaggi: 1627
Iscrizione: giovedì 12 ottobre 2006, 11:34

Re: Catturare output comando python in variabile python

Messaggio da nuzzopippo »

Domanda moolto difficile, almeno per me ... possibile soluzione trovata qui

Testata nello IDLE pare funzionare, vedi giù

Codice: Seleziona tutto

Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "copyright", "credits" or "license()" for more information.
>>> import io
>>> import sys
>>> stdout = sys.stdout
>>> s = io.StringIO()
>>> sys.stdout = s
>>> help('modules')

Warning (from warnings module):
  File "/usr/lib/python3/dist-packages/UnityTweakTool/__init__.py", line 40
    from gi.repository import Gtk
PyGIWarning: Gtk was imported without specifying a version first. Use gi.require_version('Gtk', '3.0') before import to ensure that the right version gets loaded.

Warning (from warnings module):
  File "/usr/lib/python3/dist-packages/matplotlib/cbook/deprecation.py", line 106
    warnings.warn(message, mplDeprecation, stacklevel=1)
MatplotlibDeprecationWarning: The mpl_toolkits.axes_grid module was deprecated in version 2.1. Use mpl_toolkits.axes_grid1 and mpl_toolkits.axisartist provies the same functionality instead.
>>> s.seek(0)
>>> sys.stdout = stdout
>>> help_string = s.read()
>>> print(help_string)
lease wait a moment while I gather a list of all available modules...

[Errno 17] File già esistente: '/home/nuzzopippo/.config/classicmenu-indicator'
DEBUG:pip.vcs:Registered VCS backend: git
DEBUG:pip.vcs:Registered VCS backend: hg
... ed un sacco di altra roba
Fatti non foste a viver come bruti ...
Avatar utente
omegaub
Scoppiettante Seguace
Scoppiettante Seguace
Messaggi: 342
Iscrizione: martedì 2 dicembre 2008, 12:50
Desktop: gnome
Distribuzione: Ubuntu 18.04.4 LTS
Sesso: Femminile
Località: Boschi romani

Re: Catturare output comando python in variabile python

Messaggio da omegaub »

Grazie mille!! Provo subito!! ;) :ciao:
Avatar utente
omegaub
Scoppiettante Seguace
Scoppiettante Seguace
Messaggi: 342
Iscrizione: martedì 2 dicembre 2008, 12:50
Desktop: gnome
Distribuzione: Ubuntu 18.04.4 LTS
Sesso: Femminile
Località: Boschi romani

Re: [RISOLTO]Catturare output comando python in variabile py

Messaggio da omegaub »

;) :ciao: Ha funzionato!! Grazie ancora!
Avatar utente
crap0101
Rampante Reduce
Rampante Reduce
Messaggi: 8242
Iscrizione: martedì 30 ottobre 2007, 6:33
Desktop: LXDE
Distribuzione: Ubuntu 18.04.1 LTS
Sesso: Maschile
Località: TO
Contatti:

Re: [Risolto]Catturare output comando python in variabile py

Messaggio da crap0101 »

limitatamente all'output di help() in alternativa puoi usare

Codice: Seleziona tutto

inspect.getdoc
o anche

Codice: Seleziona tutto

pydoc.render_doc
http://www.gnu.org/ http://boinc.berkeley.edu/ http://www.python-it.org/
- Ricorda le ultime parole di suo padre: «Sta' alla larga dalle chiese, figlio. La sola cosa per cui hanno la chiave è il merdaio. E giurami che non porterai mai un distintivo della legge» - W.S. Burroughs
Scrivi risposta

Ritorna a “Programmazione”

Chi c’è in linea

Visualizzano questa sezione: DoctorStrange, TommyB1992 e 10 ospiti