您可以尝试以下方法:
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
我把它留给你,如果有任何问题请告诉我..