python获取子进程的返回值

时间:2023-03-25 22:22:20
用subprocess popen 的communicate
比如,用gdalinfo命令查看一个tiff文件的信息
x = subprocess.Popen(["gdalinfo", fname], stdout=subprocess.PIPE).communicate()[0]
print(x)
b'Driver: GTiff/GeoTIFF\nFiles: 15NOV14WV031000014NOV15135121-P1BS-500171606160_05_P005_________AAE_0AAAAABAABC0.tif\nSize is 27648, 29696\nCoordinate System is `\'\nGCP Projection = \nGEOGCS["WGS 84",\n    DATUM["WGS_1984",\n        SPHEROID["WGS 84",6378137,298.257223563,\n            AUTHORITY["EPSG","7030"]],\n        AUTHORITY["EPSG","6326"]],\n    PRIMEM["Greenwich",0],\n    UNIT["degree",0.0174532925199433],\n    AUTHORITY["EPSG","4326"]]\nGCP[  0]: Id=1, Info=\n          (0.5,0.5) -> (-58.5358333333333,-34.5394444444444,0)\nGCP[  1]: Id=2, Info=\n          (27647.5,0.5) -> (-58.6861111111111,-34.5430555555556,0)\n
GCP[ 2]: Id=3, Info=\n
(27647.5,29695.5) -> (-58.685,-34.41,0)\nGCP[ 3]: Id=4, Info=\n
(0.5,29695.5) -> (-58.5366666666667,-34.4075,0)\nMetadata:\n AREA_OR_POINT=Area\n
NITF_ABPP=11\n NITF_CCS_COLUMN=0\n NITF_CCS_ROW=0\n NITF_CLEVEL=06\n NITF_CSDIDA_DAY=15\n
NITF_CSDIDA_MONTH=NOV\n NITF_CSDIDA_OPERATION=000\n NITF_CSDIDA_PASS=10\n NITF_CSDIDA_PLATFORM_CODE=WV\n
NITF_CSDIDA_PROCESS_TIME=20150112170525\n NITF_CSDIDA_PRODUCT_ID=P1\n NITF_CSDIDA_RESERVED_0=0000\n
NITF_CSDIDA_RESERVED_1=00\n NITF_CSDIDA_RESERVED_2=01\n NITF_CSDIDA_RESERVED_3=N\n
NITF_CSDIDA_RESERVED_4=N\n NITF_CSDIDA_SENSOR_ID=AA\n NITF_CSDIDA_SOFTWARE_VERSION_NUMBER=4.45.2\n
NITF_CSDIDA_TIME=20141115135121\n NITF_CSDIDA_VEHICLE_ID=03\n NITF_CSDIDA_YEAR=2014\n
NITF_CSEXRA_ALONG_SCAN_GSD=020.2\n NITF_CSEXRA_ANGLE_TO_NORTH=090.000\n NITF_CSEXRA_AZ_OF_OBLIQUITY=027.124\n
NITF_CSEXRA_A_S_VERT_GSD=N/A\n NITF_CSEXRA_CIRCL_ERR=016\n NITF_CSEXRA_CROSS_SCAN_GSD=019.8\n
NITF_CSEXRA_C_S_VERT_GSD=N/A\n NITF_CSEXRA_DYNAMIC_RANGE=01968\n NITF_CSEXRA_GEO_MEAN_GSD=020.0\n
NITF_CSEXRA_GEO_MEAN_VERT_GSD=N/A\n NITF_CSEXRA_GRD_COVER=9\n NITF_CSEXRA_GSD_BETA_ANGLE=091.3\n
NITF_CSEXRA_LINEAR_ERR=016\n NITF_CSEXRA_MAX_GSD=020.2\n NITF_CSEXRA_NUM_LINES=0029696\n
NITF_CSEXRA_NUM_SAMPLES=27648\n NITF_CSEXRA_OBLIQUITY_ANGLE=12.079\n NITF_CSEXRA_PREDICTED_NIIRS=5.0\n
NITF_CSEXRA_SENSOR=PAN\n NITF_CSEXRA_SNOW_DEPTH_CAT=9\n NITF_CSEXRA_SUN_AZIMUTH=063.557\n
NITF_CSEXRA_SUN_ELEVATION=+61.363\n NITF_CSEXRA_TIME_FIRST_LINE_IMAGE=49881.698189\n
NITF_CSEXRA_TIME_IMAGE_DURATION=00002.241200\n NITF_ENCRYP=0\n NITF_FBKGC=126,126,126\n
NITF_FDT=20151203163639\n NITF_FHDR=NITF02.10\n NITF_FSCLAS=U\n NITF_FSCLSY=US\n NITF_FSCOP=00000\n
NITF_FSCPYS=00000\n NITF_FSCTLH=0\n NITF_FTITLE=15NOV14WV031000014NOV15135121-P1BS-500171606160_05_P005 AAE 0AAAAABAABC0\n NITF_IALVL=0\n NITF_IC=C8\n NITF_ICAT=VIS\n NITF_ICORDS=G\n NITF_IDATIM=20141115135121\n NITF_IDLVL=1\n NITF_IGEOLO=343222S0583209W343235S0584110W342436S0584106W342427S0583212W\n NITF_IID1=P104AED200\n NITF_IID2=15NOV14WV031000014NOV15135121-P1BS-500171606160_05_P005 AAE 0AAAAABAABC0\n NITF_ILOC_COLUMN=0\n NITF_ILOC_ROW=0\n NITF_IMAG=1.0 \n NITF_IMAGE_COMMENTS=The imagery and metadata data on this media is the property of DigitalGlobe and is licensed for use only. All use must be in accordance with the terms of the license that accompanies the media. If the license is purchased under contract NMA 301 99 D003, use is in accordance with the license therein \n NITF_IMODE=B\n NITF_IREP=MONO\n NITF_ISCLAS=U\n NITF_ISCLSY=US\n NITF_ISCTLH=0\n NITF_ISORCE=DigitalGlobe\n NITF_OSTAID=NDL-W\n NITF_PIAIMC_CLOUDCVR=000\n NITF_PIAIMC_COMGEN=00\n NITF_PIAIMC_ESD=Y\n NITF_PIAIMC_GENERATION=1\n NITF_PIAIMC_IDATUM=WGE\n NITF_PIAIMC_MEANGSD=00020.0\n NITF_PIAIMC_PREPROC=1R\n NITF_PIAIMC_SATTRACK=00000000\n NITF_PIAIMC_SENSMODE=PUSHBROOM\n NITF_PIAIMC_SENSNAME=WV03\n NITF_PIAIMC_SOURCE=DigitalGlobe Acquired Image\n NITF_PIAIMC_SRP=Y\n NITF_PJUST=R\n NITF_PVTYPE=INT\n NITF_STDIDC_ACQUISITION_DATE=20141115135121\n NITF_STDIDC_COUNTRY=AR\n NITF_STDIDC_END_COLUMN=027\n NITF_STDIDC_END_ROW=00029\n NITF_STDIDC_END_SEGMENT=AA\n NITF_STDIDC_LOCATION=3432S05837W\n NITF_STDIDC_MISSION=WV03\n NITF_STDIDC_OP_NUM=000\n NITF_STDIDC_PASS=10\n NITF_STDIDC_REPLAY_REGEN=000\n NITF_STDIDC_REPRO_NUM=00\n NITF_STDIDC_START_COLUMN=001\n NITF_STDIDC_START_ROW=00001\n NITF_STDIDC_START_SEGMENT=AA\n NITF_STYPE=BF01\n NITF_USE00A_ANGLE_TO_NORTH=090\n NITF_USE00A_DYNAMIC_RANGE=02046\n NITF_USE00A_MAX_LP_SEG=028720\n NITF_USE00A_MEAN_GSD=020.0\n NITF_USE00A_N_REF=00\n NITF_USE00A_N_SEG=001\n NITF_USE00A_OBL_ANG=12.08\n NITF_USE00A_REV_NUM=01393\n NITF_USE00A_ROLL_ANG=+05.75\n NITF_USE00A_SUN_AZ=063.6\n NITF_USE00A_SUN_EL=+61.4\nImage Structure Metadata:\n COMPRESSION=LZW\n INTERLEAVE=BAND\nRPC Metadata:\n HEIGHT_OFF=31\n HEIGHT_SCALE=500\n LAT_OFF=-34.4754\n LAT_SCALE=0.0688\n LINE_DEN_COEFF=1 -0.001748998 0.002867378 -0.000891891 -1.1009e-05 -3.130936e-06 -1.505779e-05 -4.256269e-05 0.0001791118 -6.411448e-05 -1.480182e-07 1.884002e-07 2.299439e-06 2.065444e-07 1.937789e-07 6.022982e-05 3.128753e-07 9.631539e-08 -1.065953e-06 1.06707e-07\n LINE_NUM_COEFF=-0.01155039 -0.02219071 1.037603 -0.0143093 0.001430638 -4.002035e-05 0.0006988066 -0.0002803232 0.008815574 -9.492739e-06 1.691069e-06 7.474433e-07 1.716209e-05 1.368658e-06 -5.536113e-05 -0.0001669293 -6.567348e-05 4.828972e-07 2.179128e-05 9.029418e-07\n LINE_OFF=14359\n LINE_SCALE=14360\n LONG_OFF=-58.611\n LONG_SCALE=0.0758\n SAMP_DEN_COEFF=1 0.0009596563 -0.003263896 -0.0004033469 9.097765e-06 -1.20064e-06 -9.51889e-06 -2.531124e-05 0.0001419847 -2.445755e-05 -9.716162e-08 4.391155e-08 2.657253e-06 2.810917e-08 1.262118e-07 3.400646e-06 1.128371e-07 2.805515e-08 8.26855e-07 1.966835e-08\n SAMP_NUM_COEFF=0.003072148 -1.015328 0.0001564052 0.007921286 -0.003215768 -0.0003729291 -0.0002210835 -0.002129823 1.602836e-05 5.707117e-06 -9.895511e-06 3.067685e-05 0.0001623712 2.474425e-05 -1.358906e-05 -0.0002529502 -5.387624e-07 -1.077882e-06 7.690791e-06 -1.855927e-07\n SAMP_OFF=13615\n SAMP_SCALE=13617\nCorner Coordinates:\nUpper Left ( 0.0, 0.0)\nLower Left ( 0.0,29696.0)\nUpper Right (27648.0, 0.0)\nLower Right (27648.0,29696.0)\nCenter (13824.0,14848.0)\nBand 1 Block=256x256 Type=UInt16, ColorInterp=Gray\n Overviews: 13824x14848, 6912x7424, 3456x3712, 1728x1856, 864x928\n Image Structure Metadata:\n NBITS=11\n'