3

I have a folder D:/test/src in which have lots of excel files, I want to add one more column date which is 2019-08-01 in each one and save them into another folder D:/test/dst.

Here is what I have done. It works, but a little bit slow. So if you have quicker or others ideas, welcome to share. Thanks at advance.

import pandas as pd
import os
import glob

src = "D:/test/src/*.xls*"
dst = "D:/test/dst/"

dfs = []
for file in glob.glob(src):
    df = pd.read_excel(file)
    df['date'] = "2019-08-01"
    df["date"] = df["date"].astype(str)
    df.to_excel(os.path.join(dst, os.path.basename(file)),
                index=False)
    dfs.append(df)
2
  • use multithreading Commented Aug 2, 2019 at 7:54
  • Thanks, could you show this in my example? Commented Aug 2, 2019 at 8:06

1 Answer 1

2

Use threading:

import glob
import threading
import pandas as pd

src = "D:/test/src/*.xls*"
dst = "D:/test/dst/"

def update(excel_file):
    df = pd.read_excel(excel_file)
    df['date'] = "2019-08-01"
    df["date"] = df["date"].astype(str)
    df.to_excel(os.path.join(dst, os.path.basename(excel_file)), index=False)

for file in glob.glob(src):
    threading.Thread(target=update, args=(file,)).start()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.