不再在pycharm上获取输出文件。
我以前有一个输出文件,但是不会随着“ data_to_write”中的更改而更新。我将输出文件移到了另一个位置,但没有更改我需要生成的输出文件的输出目录路径。 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()
代码比这更长,但这是我需要创建的简单.txt文件。昨晚我得到了一个输出,但是它不会随着更改的代码而更新,因此我添加了一个“ 1”以结束(希望)在输出目录中创建一个新文件。我没有更改输出目录的相对路径。其余代码很长,因此我没有上载它。调试时没有红色错误,只有6个弱警告。
谢谢您的帮助。**代码在pycharm上完美缩进,但不会在每行中添加4个空格。.
我认为,某些事情改变了您Pycharm环境的当前工作目录。
只需在with open(
语句之前添加以下行。它将打印出当前的工作目录。然后,我建议您使用文件浏览器并查看该目录,以查看是否确实没有创建文件]
print("I will create the file ", sdsu_out, "in the directory ", os.path.realpath(os.path.getcwd())
并添加行
import os
如果尚未导入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()