脚本循环遍历目录中的文件

时间:2015-06-28 13:06:28

标签: python ogr

我有以下代码,它使用我需要的数据从shp.file创建我需要的txt文件。我有一个名为profiles的文件夹,其中包含一些名为(profil1.shp,profil2.shp,profil3.shp等)的形状文件。我想知道如何创建一个循环,以便脚本为每个文件创建一个具有相同名称的txt文件(例如,对于profil1.shp创建profil1.txt,profil2.shp创建profil2.txt等等)。

import ogr, os, sys, osr

os.chdir('..\profiles')

file = open('profil1.txt', 'w')

driver = ogr.GetDriverByName('ESRI Shapefile')

datasource = driver.Open('profil1.shp', 0)
if datasource is None:
  print 'Could not open file'
  sys.exit(1)

layer = datasource.GetLayer()

feature = layer.GetNextFeature()
while feature:

  id = feature.GetFieldAsString('ID')
  Distanta = feature.GetFieldAsString('DIST')
  Z = feature.GetFieldAsString('Z')
  geom = feature.GetGeometryRef()
  x = str(geom.GetX())
  y = str(geom.GetY())

  file.write(id + " " + Distanta + " " + "[X]:" + " " + x + ' ' + '[Y]:' + " "  + y + " " + " " + "[Z]" + Z + " " +  "\n")

  feature.Destroy()
  feature = layer.GetNextFeature()

datasource.Destroy()
file.close()

编辑:代码返回无法打开的文件。Photo of the folder containing the files and their respective names. Safe to assume I am doing something wrong.

import ogr, os, sys, osr,os.path
os.chdir = ('C:\Users\Andrei\Desktop\profil3')
l = os.listdir('C:\Users\Andrei\Desktop\profil3')
for i in l:
    if i.endswith('.shp'):
        s1 = s.split('.')[0] + '.txt'
        file = open(s1, 'w')

    driver = ogr.GetDriverByName('ESRI Shapefile')
    datasource = driver.Open(i, 0)
    if datasource is None:
        print 'Could not open file'
        sys.exit(1)

    layer = datasource.GetLayer()

    feature = layer.GetNextFeature()
    while feature:

        id = feature.GetFieldAsString('ID')
        Distanta = feature.GetFieldAsString('DIST')
        Z = feature.GetFieldAsString('Z')
        geom = feature.GetGeometryRef()
        x = str(geom.GetX())
        y = str(geom.GetY())

        file.write(id + " " + Distanta + " " + "[X]:" + " " + x + ' ' + '[Y]:' + " "  + y + " " + " " + "[Z]" + Z + " " +  "\n")

        feature.Destroy()
        feature = layer.GetNextFeature()

    datasource.Destroy()
    file.close()

1 个答案:

答案 0 :(得分:0)

您可以使用os.listdir()列出当前目录中的文件和文件夹。 这将返回当前目录中所有文件的列表(或作为参数指定给它的目录,如果未指定参数,则会检查当前目录)。

然后,您可以使用.shp功能检查名称以string.endswith()结尾的文件,然后使用该文件创建新文件。

一小部分的例子 -

import os , os.path
l = os.listdir()
for i in l:
    if i.endswith('.shp'):
        s1 = s.split('.')[0] + '.txt'

最后,s1将包含扩展名为.txt的文件。

然后你可以在这个文件上做你的逻辑,并继续这样做。

完整代码看起来像 -

import ogr,os,sys,osr,os.path

os.chdir(” .. \型材) l = os.listdir() 因为我在l:     if i.endswith('。shp'):         s1 = s.split('。')[0] +'。txt'         file = open(s1,'w')

    driver = ogr.GetDriverByName('ESRI Shapefile')

    datasource = driver.Open(i, 0)
    if datasource is None:
        print 'Could not open file'
        sys.exit(1)

    layer = datasource.GetLayer()

    feature = layer.GetNextFeature()
    while feature:

        id = feature.GetFieldAsString('ID')
        Distanta = feature.GetFieldAsString('DIST')
        Z = feature.GetFieldAsString('Z')
        geom = feature.GetGeometryRef()
        x = str(geom.GetX())
        y = str(geom.GetY())

        file.write(id + " " + Distanta + " " + "[X]:" + " " + x + ' ' + '[Y]:' + " "  + y + " " + " " + "[Z]" + Z + " " +  "\n")

        feature.Destroy()
        feature = layer.GetNextFeature()

    datasource.Destroy()
    file.close()

打开文件等的更好方法是使用with语句。查看其教程{{3}}。