Advertisement
DaniilWild

Untitled

Sep 19th, 2021
780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. from os import listdir
  2. from os.path import isfile
  3. from os.path import join as joinpath
  4.  
  5.  
  6. def serch_file(path_dir):
  7.     global file_extension , serch_string , new_string , new_extension
  8.  
  9.     all_files = listdir(path_dir)
  10.  
  11.     for file in all_files:
  12.         if isfile(joinpath(path_dir, file)):
  13.             if file.endswith(file_extension): #  если фаил с нужным нам разрешением
  14.  
  15.                 replaced_content = ""
  16.                 with open(joinpath(path_dir, file), "r") as open_file: #  откроем фаил  в режиме чтения
  17.                     for line in open_file:                             # проходим по каждой строке
  18.                         if serch_string in line:                       # если совпадает с тем что мы ищем
  19.  
  20.                             words = line.split()                       # разобьем строку
  21.                             words[2] = new_string                      # выкинеем прошлый путь и запишем наш
  22.                             line = "  " + ' '.join(words) + "\n"       # соберем массив в строку
  23.                         replaced_content +=  line
  24.  
  25.                 write_file = open(joinpath(path_dir, file[:-3]  + new_extension), "w")       # перезапишем фаил
  26.                 write_file.write(replaced_content)
  27.                 write_file.close()
  28.                 print(joinpath(path_dir, file) , "-----Succes")
  29.  
  30.         else:  # если папка то
  31.             new_path_dir = joinpath(path_dir, file)
  32.             serch_file(new_path_dir)
  33.  
  34.  
  35. PATH = "C:/Users/tsarc/Desktop/Arduino/Diplom_test_motor"  # Корневая папка где искать файлы и другие папки
  36.  
  37. file_extension = '.txt'                                    # Разрешение нужным нам файлов
  38. new_extension = '.dd'                                      # Новое расширение
  39. serch_string = " in"                                       # Уникальное начало строки которое мы ищем
  40. new_string = "'Лол/новый/путь'"                            # Новый путь который перезапишет старый
  41.  
  42. serch_file(PATH)
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement