Search code examples
pythonpandasglobargvsys

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


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.


Solution

  • 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