python

파이썬 shutil 라이브러리로 쉽게 파일 복사하기

햇농nongnong 2022. 11. 11. 14:54

파이썬 shutil 라이브러리로 간단하게 파일을 복사할 수 있다.

 

shutil.copyfile('복사할 파일이 있는 경로 주소', '복사물을 위치할 경로 주소')

 

* 파일명이 아니라 경로 path 주소

* 경로는 절대 경로를 써주는 것이 좋다.

 

 

예시)

import shutil

file_list = os.listdir('/home/haeyoung/nia12/nia_dataset/data_preprocess/fr_data/')

for file in file_list :
    file = file.replace('.wav','.txt')
    g2pfilename = 'g2p' + file
    if file in fr_data_g2pincluded :
        print(file, "is here and moved.")
        before = '/home/haeyoung/nia12/nia_dataset/data_preprocess/fr_data_g2pincluded/' + g2pfilename
        after = '/home/haeyoung/nia12/nia_dataset/data_preprocess/fr_data/' + file
        shutil.copyfile(before, after) 
    else :
        print(file, "is not in g2p folder")