Home > OS >  How to delete the files after the concatenation with python?
How to delete the files after the concatenation with python?

Time:12-09

Here's my code :

import glob
import pandas
import os

os.chdir("/filepath")

extension = 'xlsx'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]

combined = pd.concat([pd.read_excel(f) for f in all_filenames ])

combined.to_excel("data.xlsx", header=False, index=False)

My question is : How to delete all th files in the folder except the last one data.xslx.

Like that, I always have the last file and if I want to concat again, it just concat the new files with one file, the one who was concatened before. So I avoid duplicates.

CodePudding user response:

This should work for you, run this code after saving "data.xlsx" file:

def delete_file(filename):
    import os
    try:
        os.remove(filename)
    except OSError:
        pass

for filename in all_filenames:
    delete_file(filename)

The final Code should look like this:

import glob
import pandas as pd
import os

os.chdir("/filepath")

def delete_file(filename):
    import os
    try:
        os.remove(filename)
    except OSError:
        pass

extension = 'xlsx'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]

combined = pd.concat([pd.read_excel(f) for f in all_filenames ])

combined.to_excel("data.xlsx", header=False, index=False)

for filename in all_filenames:
    delete_file(filename)
  • Related