如何使用参数在命令提示符下读取目录(文件数量为 n)

how to read directory (files are n in number) in command prompt using arguments

目前,我在代码中传递文件名时使用参数。

用法:

!python C:/Users/sindhus/Desktop/Fio/fio_mergefiles.py 1.log 2.log 3.log .......n output.csv 

我使用的代码如下。下面代码中的“读者”我用它来传递其他功能。

debug = False
argv = ['']+['f%02d'%n for n in range(1, n)]+[''] if debug else argv
out  = stdout if debug else open(argv[-1], 'w')

readers = [reader(open(f), n) for n, f in enumerate(argv[1:-1])]
N = len(readers) 
print('Interval',*('IOPS%02d'%(n+1) for n in range(N)), sep=',', file=out)

# initialize for the loop
t0 = ""

# the loop
for t,iops,extra1,extra2,n in merge(*readers, key=lambda x:int(x[0])):
    if t != t0:
        if t0: print(t0, *iops_l, sep=',', file=out)
        t0 = t
        iops_l = ['····' if debug else '']*N
    iops_l[n] = '%4s'%iops if debug else iops

现在我打算将所有输入文件保存在文件夹中,我只想解析目录名称和 output_file 路径。我尝试使用路径列表和 glob dint 帮助很大。

我想要的是

!python C:/Users/sindhus/Desktop/Fio/fio_mergefiles.py C:/Users/data/ C:/Users/output/output.csv

我已经编辑了我的代码,因为他们中的大多数人都想要“读者”参数返回的内容。 提前致谢。

您需要使用os.listdir函数:

import os

folder_path = "your/folder/path"

for filename in os.listdir(folder_path):
    with open(os.path.join(folder_path, filename), "r") as fp:
        # ...

代码修改如下

cmd_folder_input_iops = Path(sys.argv[1])
glob_folder_iops = cmd_folder_input_iops.glob('*')
readers = [reader(open(f), n) for n, f in enumerate(glob_folder_iops)]
N=len(readers)
out_iops  = stdout if debug else open(argv[2], 'w')

# header is "Interval,IOPS01,IOPS02,...,IOPSnn" modify as you like
print('Interval',*('IOPS%02d'%(n+1) for n in range(N)), sep=',', file=out_iops)

# initialize for the loop
t0 = ""

# the loop
for t,iops,extra1,extra2,n in merge(*readers, key=lambda x:int(x[0])):

    if t != t0:
       if t0: print(t0, *iops_l, sep=',', file=out_iops)
       t0 = t
       iops_l = ['····' if debug else '']*N
    
   iops_l[n] = '%4s'%iops if debug else iops

# the last record to output
print(t0, *iops_l, sep=',', file=out_iops)`

此致, 辛杜