pythonpython-3.xparsingformattingchunked

How to unformat a filename?(Parsing or opposite of format)


I want to return the filename of the files that has been converted by this function.

from more_itertools import chunked

def file_conversion(input_file, output_file_pattern, chunksize):
    with open(input_file) as fin:
        reader = csv.DictReader(fin, delimiter=',')
        for i, chunk in enumerate(chunked(reader, chunksize)):
            with open(output_file_pattern.format(i), 'w', newline='') as fout:
                writer = csv.DictWriter(fout, reader.fieldnames, delimiter='|')
                writer.writeheader()
                writer.writerows(chunk)
                print("Successfully converted into", output_file)
script_dir = os.path.dirname(os.path.abspath(__file__))
dest_dir = os.path.join(script_dir, 'temp')
try:
     os.makedirs(dest_dir)
except OSError:
     pass
path = os.path.join(dest_dir,'out{:03}.csv' )

#this creates file as out000.csv and more out001.csv or out002.csv depending upon the number of lines

file_conversion('in.csv', path, 7000) 
base_name = os.path.basename(path)
print(base_name)  #returns out{03}.csv

This prints out{03}.csv. But I want the result of the filename that has been converted like out000.csv. Is there any-way to return this? If there are more files, how to return the name of them?


Solution

  • Ok, you need to make a collection inside your function and populate it with all the filenames used:

    def file_conversion(input_file, output_file_pattern, chunksize):
        output_filenames = []
        with open(input_file) as fin:
            reader = csv.DictReader(fin, delimiter=',')
            for i, chunk in enumerate(chunked(reader, chunksize)):
                output_filename = output_file_pattern.format(i)
                with open(output_filename, 'w', newline='') as fout:
                    output_filenames.append(output_filename)
                    writer = csv.DictWriter(fout, reader.fieldnames, delimiter='|')
                    writer.writeheader()
                    writer.writerows(chunk)
                    print("Successfully converted into", output_file)
        return output_filenames
    

    You can call it like this:

    paths = file_conversion('in.csv', path, 7000) 
    base_names = [os.path.basename(path) for path in paths]
    print(base_names)