14.08.2017 г.

Replace empty space in name with python script

Path = r'C:\Users\Borko - Home\Desktop\TEST\trii'
search_patter = ' '
import os
log_file = 'changes.log'

class Main:

    def __init__(self, Path, search_patter):
        self.Path = Path
        self.search_patter = search_patter
        ''' check if file exists only for me'''
        assert os.path.exists(self.Path) == True

    def search_for_file(self):
        result = []
        for root, firname, filename in os.walk(self.Path):
            for file in filename:
                if self.search_patter in file:
                    result.append(root + '\\' + file)
        return result

    def write_to_log_file(self):
        with open(log_file, 'w') as f:
            for file in Main.search_for_file(self):
                f.write(file + '\n')

    def change_fileName(self):
        for file in Main.search_for_file(self):
            new_filename = os.path.basename(file).replace(' ', '_')
            new_path_name = os.path.dirname(file)
            total_new = new_path_name + '\\' + new_filename
            os.rename(file, total_new)

if __name__ == "__main__":
    progress = Main(Path, search_patter)
    progress.change_fileName()

1 коментар :