首页 > 解决方案 > 使用python同时在多个文件上运行一个函数

问题描述

我有一个特定的功能,通过输入目录和文件名来操作文本文件。

定义的函数如下

def nav2xy(target_directory, target_file):

after_rows = f'MOD {target_file}_alines.txt'
after_columns = f'MOD {target_file}_acolumns.txt'

# this segment is used to remove top lines(8 in this case) for work with only the actual data
infile = open(f'{target_directory}/{target_file}', 'r').readlines()
with open(after_rows, 'w') as outfile:
    for index, line in enumerate(infile):
        if index >= 8:
            outfile.write(line)

# this segment removes the necessary columns, in this case leaving only coordinates for gmt use
with open(after_rows) as In, open(after_columns, "w") as Out:
    for line in In:
        values = line.split()
        Out.write(f"{values[4]} {values[5]}\n")

我正在寻找一种在所选目录中的所有文件上运行此代码一次的方法(可以按名称定位或只执行所有文件),我应该将函数更改为仅使用文件名吗?

尝试以这种方式运行该功能,但无济于事

for i in os.listdir('Geoseas_related_files'):
    nav2xy('target_directory', i)

这种方式非常有效,尽管我仍然会遇到这个错误。

    (base) ms-iMac:python gan$ python3 coordinates_fromtxt.py 
Traceback (most recent call last):
  File "coordinates_fromtxt.py", line 7, in <module>
    nav2xy('Geoseas_related_files', str(i))
  File "/Users/gadraifman/research/python/GAD_MSC/Nav.py", line 19, in nav2xy
Out.write(f"{values[4]} {values[5]}\n")

IndexError:列表索引超出范围

任何帮助或建议都会有很大帮助,

标签: python-3.x

解决方案


根据我从使用 Python 遍历目录中收集到的信息,循环目录的最佳方法是使用 glob。

我对您的代码进行了一些广泛的其他修改以简化它并删除将行保存到文件的中间步骤只是为了再次读取它们。如果此步骤是强制性的,请随时将其添加回来。

import os, glob

def nav2xy(target_file):

    # New file name, just appending stuff. 
    # "target_file" will contain the path as defined by root_dir + current filename
    after_columns = f'{target_file}_acolumns.txt'

    with open(target_file, 'r') as infile, open(after_columns, "w") as outfile:
        content = infile.readlines()
        #
        #                    --- Skip 8 lines here
        #                   |
        #                   v
        for line in content[8:]:
            # No need to write the lines to a file, just to read them again.
            # Process directly
            values = line.split()
            outfile.write(f"{values[4]} {values[5]}\n")

# I guess this is the dir you want to loop through. 
# Maybe an absolute path c:\path\to\files is better.
root_dir = 'Geoseas_related_files/*'

for file_or_dir in glob.iglob(os.path.join(root_dir,"*")):
    # Skip directories, if there are any.
    if os.path.isfile(file_or_dir): 
        nav2xy(file_or_dir)

推荐阅读