【发布时间】: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