Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

python - 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...