0

I have 2 directories with multiple files(read as CSV). The filename denotes a particular customer number. I read the files as follows,

for dirpath, dirs, files in os.walk("/path/to/file/"):
    for file in files:
        directory1(os.path.join(dirpath, file))

I read these files as CSV.

If a directory2 filename(file) matches a filename in directory1 then I want to append the data together as one file.

If the filename(file) is not present in directory1 then copy the data as it is.

This new files should be saved to a new directory.

Amogh Katwe
  • 196
  • 11

1 Answers1

1

Can you try the following:

import os
import glob
import shutil

dir1 = "/path/to/dir1/"
dir2 = "/path/to/dir2/"
new_dir = "/path/to/dir3/"
fpaths_dir1 = glob.glob(os.path.join(dir1, '*.csv'))
fpaths_dir1 = [os.path.basename(fpath) for fpath in fpaths_dir1]
fpaths_dir2 = glob.glob(os.path.join(dir2, '*.csv'))
fpaths_dir2 = [os.path.basename(fpath) for fpath in fpaths_dir2]

for fpath in fpaths_dir1:
    if fpath in fpaths_dir2:
        f1 = os.path.join(dir1, fpath)
        f2 = os.path.join(dir2, fpath)
        # write your code to open both files
        # and contatenate
    else:
        f1 = os.path.join(dir1, fpath)
        f3 = os.path.join(new_dir, fpath)
        shutil.copyfile(f1, f3)

append the data together as one file i leave it to you, kindly let me know if any issues..

Jeril
  • 7,858
  • 3
  • 52
  • 69