简体   繁体   中英

How to move files and folder in same rows based on a pandas/csv file?

I have a folder named "My_Videos" inside it has files and a folder named same as the file with some files and also I have a (csv file/df) with the location to move.

I have to move the files and folder based on the output location in csv file. if its location is both it should copy into both the folder else move to respective folder.

Example folder structure

在此处输入图像描述

Output location structure

在此处输入图像描述

csv file with filename,folder name and output folder name

在此处输入图像描述

I have tried this, but i dono how to move folder and files at same time

import pandas as pd
import shutil
import os

df = pd.read_csv("my_file.csv")

dest = "Script_Output" 

for _, row in df.iterrows():
    if row['location'] == both:
        source_file = os.path.join("My_videos",row['file_name'])
        dest_file = os.path.join(dest,row['file_name'])
        shutil.copy(source_file, dest_file)
    else:
        source_file = os.path.join("My_videos",row['file_name'])
        dest_file = os.path.join(dest,row['location'],row['file_name'])
        shutil.move(source_file, dest_file)

shutil.copytree(source, dest) then shutil.rmtree(source) will move files and directories from one place to another, and rmtree will remove the old tree

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM