Python file directory operation, successfully copy the excel file to the backup file

#But need to be modified

 

""" 
Use python to obtain the specified type files in a certain path folder and subfolders in batches, and store them according to the specified path 
""" 

import os 
import shutil 

soundfile = [] 


def eachfile(filepath): 
    pathdir = os.listdir( filepath) 
    for s in pathdir: 
        newdir = os.path.join(filepath, s) # Add the file name to the current file path 
        if os.path.isfile(newdir): # If it is a file 
            if os.path.splitext( newdir)[1] == ".xlsx": # If the file is a 
                soundfile with the suffix ".pdb" .append(newdir) 
        elif os.path.isdir(newdir): # If it is a path 
            eachfile(newdir) # Recursive 
    return soundfile 


fp = r'C:\Users\laiwu\PycharmProjects\pythonProject1' 

os.chdir(fp)
f = eachfile(fp)

# for i in range(len(f)):
#     print(f[i])
# print(len(f))

for i in range(len(f)):
    pcm_file = f[i]
    path_0 = os.path.split(f[i])[0]
    path_1 = os.path.split(f[i])[1]
    print(path_1)
    oldname = path_0 + '/' + path_1
    newname = r'C:\Users\laiwu\PycharmProjects\pythonProject1\bak' + '/' + path_1
    shutil.copyfile(oldname, newname)
    print(str(i) + '/' + str(len(f)))

Guess you like

Origin blog.csdn.net/jidawanghao/article/details/112463382