Moving all files from one directory to another using Python

前端 未结 9 1774
长发绾君心
长发绾君心 2020-12-24 00:57

I want to move all text files from one folder to another folder using Python. I found this code:

import os, shutil, glob

dst = \'/path/to/dir/Caches/com.app         


        
9条回答
  •  清歌不尽
    2020-12-24 01:15

    def copy_myfile_dirOne_to_dirSec(src, dest, ext): 
    
        if not os.path.exists(dest):    # if dest dir is not there then we create here
            os.makedirs(dest);
            
        for item in os.listdir(src):
            if item.endswith(ext):
                s = os.path.join(src, item);
                fd = open(s, 'r');
                data = fd.read();
                fd.close();
                
                fname = str(item); #just taking file name to make this name file is destination dir     
                
                d = os.path.join(dest, fname);
                fd = open(d, 'w');
                fd.write(data);
                fd.close();
        
        print("Files are copyed successfully")
    

提交回复
热议问题