Précédent   Forum du club des développeurs et IT Pro > Autres langages > Python & Zope
Python & Zope Forum d'entraide sur la programmation en Python et Zope. Avant de poster -> F.A.Q Python
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 06/12/2012, 19h18   #1
Nico820
Membre du Club
 
Nicolas
Ingénieur développement logiciels
Inscription : décembre 2006
Messages : 128
Détails du profil
Informations personnelles :
Nom : Nicolas
Localisation : France

Informations professionnelles :
Activité : Ingénieur développement logiciels
Secteur : Aéronautique - Marine - Espace - Armement

Informations forums :
Inscription : décembre 2006
Messages : 128
Points : 65
Points : 65
Par défaut Problème encodage stdout

Bonjour,

heureux débutant sur Python, je cherche à récupérer les données en sortie d'un programme sur un fichier texte.

Malheureusement, toutes les lignes en sortie sont crées, comme s'il n'existait pas de retour à la ligne.

Comment formater ou encoder le texte en sortie pour qu'on retrouve les retours à la ligne, comme lorsqu'on lance le programme sous windows avec cmd?

Ci-dessous le code:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
 
import glob
import subprocess
import sys
import os
 
sys.stdout = open(u'stdout2.txt','w')
 
process = subprocess.Popen(r"C:\temp\program -h", stdin=None, stdout=subprocess.PIPE, stderr= subprocess.STDOUT, shell=True)
 
outputlines = process.stdout.readlines()
print(outputlines)
Merci d'avance,

Nicolas
Nico820 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 06/12/2012, 20h35   #2
wiztricks
Expert Confirmé Sénior
 
Inscription : juin 2008
Messages : 3 693
Détails du profil
Informations forums :
Inscription : juin 2008
Messages : 3 693
Points : 4 521
Points : 4 521
Salut,

Avec une version Python 3+, subprocess.PIPE est une sequence de "bytes".
Et le soucis Windows étant le traitement des fin de lignes '\r\n'.
Thus:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
>>> from subprocess import Popen, PIPE
>>> 
>>> p = Popen('DIR/?', stdout=PIPE, shell=True, universal_newlines=True)
>>> lines = p.stdout.read().split('\n')
>>> for e in lines:
...       print (e)
...
Displays a list of files and subdirectories in a directory.
 
DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
  [/O[[:]sortorder]] [/P] [/Q] [/R] [/S] [/T[[:]timefield]] [/W] [/X] [/4]
 
  [drive:][path][filename]
              Specifies drive, directory, and/or files to list.
 
  /A          Displays files with specified attributes.
  attributes   D  Directories                R  Read-only files
               H  Hidden files               A  Files ready for archiving
               S  System files               I  Not content indexed files
               L  Reparse Points             -  Prefix meaning not
  /B          Uses bare format (no heading information or summary).
  /C          Display the thousand separator in file sizes.  This is the
              default.  Use /-C to disable display of separator.
  /D          Same as wide but files are list sorted by column.
  /L          Uses lowercase.
  /N          New long list format where filenames are on the far right.
  /O          List by files in sorted order.
  sortorder    N  By name (alphabetic)       S  By size (smallest first)
               E  By extension (alphabetic)  D  By date/time (oldest first)
               G  Group directories first    -  Prefix to reverse order
  /P          Pauses after each screenful of information.
  /Q          Display the owner of the file.
  /R          Display alternate data streams of the file.
  /S          Displays files in specified directory and all subdirectories.
  /T          Controls which time field displayed or used for sorting
  timefield   C  Creation
              A  Last Access
              W  Last Written
  /W          Uses wide list format.
  /X          This displays the short names generated for non-8dot3 file
              names.  The format is that of /N with the short name inserted
              before the long name. If no short name is present, blanks are
              displayed in its place.
  /4          Displays four-digit years
 
Switches may be preset in the DIRCMD environment variable.  Override
preset switches by prefixing any switch with - (hyphen)--for example, /-W.
- W
__________________
Architectures Post-Modernes
wiztricks est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 20h59.


 
 
 
 
Partenaires

Hébergement Web