0

df1:

   Country   Product
0  Columbia  Shoes
1  Brazil    Football

folder1_files: 

col_sho.csv
Bra_foo.csv
Gha_cof.csv

fold1_path='c:/folder1'
fold2_path='c:/foder2'

I need to move some files from folder 1 to folder2 based on the information from df1. You can see in df1 there are two countries and Product.Based on that information col_shoe.csv and bra_foo.csv files needs to copied in the folder2.

K RAJA
  • 19
  • 6
  • 1
    To get the filename from the DataFrame object you can try: `files = df.apply(lambda x: ('_'.join((x.str[:3]).str.lower())+'.csv'),axis=1).tolist()` will give you list of filenames like`['bra_foo.csv', 'col_sho.csv']` Then you can use `os.rename('folder1/'+files[0],'folder2/'+files[0])` – JenilDave Sep 24 '20 at 05:58
  • 1
    Does this answer your question? [How to move a file?](https://stackoverflow.com/questions/8858008/how-to-move-a-file) – woblob Sep 24 '20 at 05:59

1 Answers1

2

Using clear names for files will help in moving file easy.

import shutil
import os
import pandas as pd
folder1 = "./folder1"
folder2 = "./folder2"


df = pd.DataFrame({"Country":["Columbia", "Brazil"], "Product":["Shoes","Football"]})
s = df["Country"].str[:3] + "_" + df["Product"].str[:3] + ".csv"
for file_name in s.values:
    if os.path.isfile(folder1 + os.path.sep + file_name):
        shutil.move(folder1 + os.path.sep + file_name, folder2 + os.path.sep + file_name)
    else:
        print("file {} is not available".format(file_name))

If you want to use lower case letters only, then you can use

df["Country"].str[:3].str.lower()
goutham
  • 36
  • 3