【问题标题】:Running a function on multiple files simultaneously with python使用python同时在多个文件上运行一个函数
【发布时间】:2019-12-16 04:45:29
【问题描述】:

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

定义函数如下

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: 列表索引超出范围

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

【问题讨论】:

  • 你想循环单个目录还是递归地向下一棵树?
  • sing dir,似乎可以与 os.listdir 一起使用,因为我将文件更改为 i(愚蠢的错误),尽管除了工作之外,我仍然遇到上述错误

标签: python-3.x


【解决方案1】:

根据我从Iterating through directories with 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)

【讨论】:

    猜你喜欢
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    相关资源
    最近更新 更多