【问题标题】:Loop through files and apply function in Python循环文件并在 Python 中应用函数
【发布时间】:2019-09-15 12:06:24
【问题描述】:

我有一些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)  

【问题讨论】:

    标签: python operating-system subprocess


    【解决方案1】:

    首先,您可以将所有文件名保存在一个列表中,例如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)  
    

    【讨论】:

    • 太棒了。乐于助人。
    【解决方案2】:

    您可以将所有文件保存到一个列表中,然后对其进行迭代。

    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)
    

    【讨论】:

    • NameError: name 'input_file' 未定义。我认为您错过了定义input_fileoutput_file 的部分。
    • 我解决了这个问题。我只需要将input_file 替换为dex_file
    【解决方案3】:

    您可以通过将文件迭代代码中的打印函数替换为转换函数来做到这一点。

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2018-07-23
      • 2017-11-06
      • 1970-01-01
      相关资源
      最近更新 更多