others - 我不能在pycharm中的python代码上获取输出文件

不能在pycharm上获取输出文件。

将输出文件移到另一个位置,但是没有更改输出文件所需的输出目录路径,Pycharm不再创建输出文件。


def main():


driver = gdal.GetDriverByName('JPEG')


driver.Register()



sdsu_in = './data/SDSU_3Color.jpg'


# open and read only jpeg file


data = gdal.Open(sdsu_in, gdalconst.GA_ReadOnly)



if data is None:


 print("Error: Could not read '{}'".format(sdsu_in))


 sys.exit()



rows = data.RasterYSize


cols = data.RasterXSize


r_bands = data.RasterCount


proj = data.GetProjectionRef()


m_data = data.GetMetadata()


geotrans = data.GetGeoTransform()


x_origin = geotrans[0]


y_origin = geotrans[3]


pixel_width = geotrans[1]


pixel_height = geotrans[5]



print("The # of rows: {}, The # of columns: {}, raster bands: {},"


"metadata: {}".format(rows, cols, r_bands, proj, m_data))



data_to_write = f'The number of rows: {rows}n The number of columns 


{cols}n The number of raster bands: {r_bands}n The projection: {proj}n 


The metadata: {m_data}n The Geotransform information: {geotrans}n X 


Origin: {x_origin}n Y Origin: {y_origin}n Pixel Width: {pixel_width}n 


Pixel Height: {pixel_height}'



# creating new output with specified information


sdsu_out = './output/output_w1.txt'


with open(sdsu_out, 'w') as f:


 f.write(data_to_write)


 f.flush()


 f.close()



if __name__ == '__main__':


 main()



我没有改变输出目录的相对路径,其他的代码很长,所以我没有上传,调试中没有红色错误,只有6个弱警告。

时间:

我认为,有东西改变了你Pycharm环境的当前工作目录。

with open(语句之前直接添加以下行,它将打印出目前的工作目录。


print("I will create the file", sdsu_out,"in the directory", os.path.realpath(os.path.getcwd())



并添加行

 
import os



 


from osgeo import gdal, gdalconst


import sys


import random


import os


import numpy as np



def main():



 driver = gdal.GetDriverByName('JPEG')


 driver.Register()



 sdsu_in = './data/SDSU_3Color.jpg'


 data = gdal.Open(sdsu_in, gdalconst.GA_ReadOnly)



 if data is None:


 print("Error: Could not read '{}'".format(sdsu_in))


 sys.exit()



 rows = data.RasterYSize


 cols = data.RasterXSize


 r_bands = data.RasterCount


 proj = data.GetProjectionRef()


 m_data = data.GetMetadata()


 geotrans = data.GetGeoTransform()


 x_origin = geotrans[0]


 y_origin = geotrans[3]


 pixel_width = geotrans[1]


 pixel_height = geotrans[5]



 print("The # of rows: {}, The # of columns: {}, raster bands: {},"


"metadata: {}".format(rows, cols, r_bands, proj, m_data))



 data_to_write = f'The number of rows: {rows}n The number of ' 


 f'columns: {cols}n The number of raster bands: {r_bands}n The ' 


 f'projection: {proj} The metadata: {m_data}n The Geotransform ' 


 f'information: {geotrans}n X Origin: {x_origin}n Y Origin: ' 


 f'{y_origin}n Pixel Width: {pixel_width} Pixel Height: ' 


 f'{pixel_height}'



 # creating new output with specified information


 # relative path


 sdsu_out = './output/output_wozniak.txt'


 # tried absolute path to get output but not luck :(


 sdsu_out1 = 'C:/Users/sarah/PycharmProjects/GEOG582/a08_wozniak/output/output_wozniak1.txt'



 print("I will create the file", sdsu_out,"in the directory", os.path.realpath(os.path._getfullpathname()))


 with open(sdsu_out, 'w') as f:


 f.write(data_to_write)


 # f.flush()


 # f.close()



 if __name__ == '__main__':


 main()



...