我使用下面的代码将一个文件分成几百个不同的文件,每个文件有16行。现在我想多次运行此代码,并在不同的文件夹中读取输入文件和写入输出,而不仅仅是在当前文件夹中。现在我运行代码为:
python split.py < input.txt
但我想做一些类似的事情:
for i in {1..10}; do python split.py < folder${i}/input.txt ; done
并将这些文件写入相应的文件夹中。我可以从不同的文件夹中读取输入文件,但问题是输出文件在我运行命令的目录中被写入和覆盖。我不知道如何在代码中修复这个问题。你知道吗
这是我的短代码:
#!/usr/bin/env python
from __future__ import print_function
import sys, os
import itertools
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
args = [iter(iterable)] * n
return itertools.izip_longest(fillvalue=fillvalue, *args)
def convNotation(x):
if isinstance(x, float):
s = str(x)
else:
s = x
return s.replace('e', "*^")
Values = (Line.strip() for Line in sys.stdin)
Data = list(grouper(Values, 16, 0.0))
for i in range(len(Data)):
Conf = Data[i]
with open("ratio-{}.txt".format(i), 'w') as File:
for x in Conf:
print(x, file=File)
你可以用
我假设所有的输入都在同一个目录下,格式为*.txt。 您需要创建新的顺序目录并将输出放在那里。您可以尝试在shell脚本中运行代码,而不是更改python代码。你知道吗
伪代码如下所示:
好的。我想我现在明白了。我可以做到:
谢谢大家。你知道吗
相关问题 更多 >
编程相关推荐