Перебирайте файлы и применяйте функцию в Python - PullRequest
0 голосов
/ 26 апреля 2019

У меня есть dxf файлы, и я хочу преобразовать их в geojson файлы:

import subprocess
from subprocess import call
import os

working_directory = 'D:/dxf_files/'

for subdir, dirs, files in os.walk(working_directory):
    for file in files:
        if file.endswith('.dxf'):
            print(file)

Выход:

BJ-SZZDS-1010084246-dongta-11.dxf
BJ-SZZDS-1010084246-dongta-12.dxf
BJ-SZZDS-1010084246-dongta-17.dxf
BJ-SZZDS-1010084246-dongta-18.dxf
BJ-SZZDS-1010084246-dongta-19.dxf
...

Я хочу поместить каждый из этих файлов в input_file ниже, оставив output_file имя файла таким же, как input_file, заменив расширение файла. Теперь два блока кода разделены, как я могу объединить их вместе? Спасибо за любую помощь заранее.

input_file = 'BJ-SZZDS-1010084246-dongta-11.dxf'
output_file = 'BJ-SZZDS-1010084246-dongta-11.geojson'

def dxf2geojson(output_file, input_file):
    command = ['ogr2ogr', '-f', 'GeoJSON', output_file, input_file]
    p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    return p
dxf2geojson(output_file, input_file)  

Ответы [ 3 ]

1 голос
/ 26 апреля 2019

Вы можете сохранить все файлы в списке, а затем выполнить итерацию по нему.

import subprocess
from subprocess import call
import os

working_directory = 'D:/dxf_files/'
def_list = []

for subdir, dirs, files in os.walk(working_directory):
  for file in files:
    if file.endswith('.dxf'):
      dxf_list.append(file)


def dxf2geojson(output_file, input_file):
  command = ['ogr2ogr', '-f', 'GeoJSON', output_file, input_file]
  p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  return p


for  dex_file in dexf_list:
  output_file = dex_file[:-4] + '.geojson'
  dxf2geojson(output_file, dex_file)
1 голос
/ 26 апреля 2019

Это можно сделать, заменив функцию печати в коде итерации файла на функцию преобразования.

import subprocess
from subprocess import call
import os

working_directory = 'D:/dxf_files/'

for subdir, dirs, files in os.walk(working_directory):
    for file in files:
        if file.endswith('.dxf'):
            input_file = file
            output_file = file[:-3]+'geojson'
            P = dxf2geojson(output_file, input_file)
1 голос
/ 26 апреля 2019

Сначала вы можете сохранить все имена файлов в списке, например, file_list:

import subprocess
from subprocess import call
import os

working_directory = 'D:/dxf_files/'

file_list = []   # define file_list to save all dxf files
for subdir, dirs, files in os.walk(working_directory):
    for file in files:
        if file.endswith('.dxf'):
            file_list.append(file)   # save the filenames in file_list

Затем выполните каждый файл из file_list:

def dxf2geojson(output_file, input_file):
    command = ['ogr2ogr', '-f', 'GeoJSON', output_file, input_file]
    p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    return p

for input_file in file_list:
    f = input_file[:-4]  # to omit .dxf
    output_file = f + '.geojson'    # add file extension .geojson
    dxf2geojson(output_file, input_file)  
...