-1

currently, I am using arguments while passing the filenames in the code.

usage :

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

The code I am using is as below. The "readers" in the below code I am using it to pass for other function.

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

now I am planning to keep all the input files in the folder and I want to parse only the directory name and the output_file path. I tried using path list and glob dint help much.

what I want is

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

I have edited my code as most of them wanted what the "readers" parameter was returning. Thanks in advance.

Sindhu
  • 15
  • 4

2 Answers2

0

You need to use os.listdir function:

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:
        # ...
dukkee
  • 1,112
  • 1
  • 9
  • 17
  • if I use the above code then in readers = I have argv[1:-1] .. What should be changed there... – Sindhu Feb 01 '21 at 10:47
0

The code is changed as below

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)`

Regards, Sindhu

Sindhu
  • 15
  • 4