ah bon
ah bon

Reputation: 10011

Loop through files and apply function in Python

I have some dxf files and want to transform them to geojson files:

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)

Output:

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
...

I want put each of these files in input_file below, keep output_file files name same as input_file by replacing files' extension. Now two block of code is seperated, how can I combine them together? Thanks for any help at advance.

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)  

Upvotes: 0

Views: 1412

Answers (3)

YusufUMS
YusufUMS

Reputation: 1493

First, you can save all filenames in a list, for example, 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

Then, execute each file from the 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)  

Upvotes: 1

razdi
razdi

Reputation: 1440

You can save all the files to a list and then iterate over it.

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)

Upvotes: 1

ShivamPR21
ShivamPR21

Reputation: 119

You can do this by replacing your print function in file iteration code to conversion function.

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)

Upvotes: 1

Related Questions