简体   繁体   中英

how to rename files to sub directory

I am trying to rename all my .wav files in my sub directory with a tag of sub directory they are present in. Fox example directory/sub-directory1/ 1_1.wav to directory/sub-directory1/ sdir1_1.wav . I know how to rename files in python but I cannot loop through the subdirectory and then add the tag. although the code below works to align sub dir and files but it wont loop through all the files because for dire in dirs: will not work if the files are more than the dirs

 import os

    rootdir = r'C:\Users\test'

    for subdir, dirs, files in os.walk(rootdir):

        for dire in dirs:
          for file in files:
           filepath = subdir+os.sep+file

           if filepath.endswith('.wav'):
             print (dire+ file) 

I have tested this and it works.

import shutil
import os
from glob import glob

# Define your source folder.
source_dir = 'F:\\Test\\in\\'
# Define your target folder.
target_dir = 'F:\\Test\\out\\'
# Define the file extension you want to search for.
file_ext = '*.mp4'
# use glob to create a list of files in your source DIR with teh desired extension.
file_check = glob(source_dir + file_ext)


# For each item in file_check shuttle will copy teh source file and write it renamed to your target location.
for i in file_check:

    shutil.copy(i, target_dir + 'dir_out_' + os.path.basename(i)) 
    #os.path.basename gives us just the filename and extension minus the absolute path.
    #i,e test123456.mp4

Here are the contents of the target dir:

F:\Test\out\dir_out_test_10.mp4
F:\Test\out\dir_out_test_2.mp4
F:\Test\out\dir_out_test_3.mp4
F:\Test\out\dir_out_test_4.mp4
F:\Test\out\dir_out_test_5.mp4
F:\Test\out\dir_out_test_6.mp4
F:\Test\out\dir_out_test_7.mp4
F:\Test\out\dir_out_test_8.mp4
F:\Test\out\dir_out_test_9.mp4

Check out the shutil and glob if you want to do a file system move instead of a copy use shutil.move() instead of shutil.copy() .

EDIT:

Python 3.5+

Here is how to find all files in a root DIR:

glob('F:\\test\\**\\*.mp4', recursive=True)

This will find all the files the root DIR and sub folders, then you can use the shutil method above to do with them what you want.

After some trial and error I was able to achieve the result.

 import os

    rootdir = r'C:\Users\test'

    for dirpath, dirnames, filenames in os.walk(rootdir):

        for file in filenames:
         filepath = dirpath +os.sep+file
         if filepath.endswith('wav'):
                 # split directory filepath
                 split_dirpath = dirpath.split(os.sep)
                 # take the name of the last directory which I want to tag
                 dir_tag = (split_dirpath[-1])
                 # wasn't necessary to split the extension, still....
                 f_name, f_ext = (os.path.splitext(file))
                 # add the tag of the sub dir
                 f_name= dir_tag +'_'+ f_name
                 # create the new file name
                 new_name = f_name +f_ext
                 print(new_name)
                 os.rename(filepath,dirpath+os.sep+new_name)

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