jimMoonrock

Untitled

Apr 23rd, 2021
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. import os
  2. import shutil
  3.  
  4.  
  5. class work_with_files:
  6.     def __init__(self, path):
  7.         self.path = path
  8.  
  9.     def rename_file(self):
  10.  
  11.         try:
  12.  
  13.             if os.path.dirname(self.path):
  14.                 files_in_folder = os.listdir(self.path)
  15.                 print(files_in_folder)
  16.                 if len(files_in_folder) != 0:
  17.  
  18.                     for file_txt in files_in_folder:
  19.  
  20.                         user_choice = input("если вы хотите просто перенсти файл в другуб папку введите - NO, а если "
  21.                                                 "хотите перемиминовать файл прежде чем пренести введите - Yes>> ")
  22.                         if user_choice == "Yes":
  23.                             inp = input("Enter the namefile")
  24.                             os.rename(f"{self.path}\{file_txt}", f"{self.path}\{inp}.txt")
  25.  
  26.                         elif user_choice == "NO":
  27.                             os.rename(f"{self.path}\{file_txt}", f"{self.path}\{file_txt}")
  28.  
  29.         except FileNotFoundError:
  30.                 pass
  31.  
  32.     def move_in_bucket(self):
  33.         files_in_folder = os.listdir(self.path)
  34.         for renamed_files in files_in_folder:
  35.             shutil.move(f"{self.path}\{renamed_files}", "C:\computer_busket")
  36.  
  37.     def watch_text_in_file(self):
  38.         files_in_folder = os.listdir(self.path)
  39.         for files in files_in_folder:
  40.             with open(f"{self.path}\{files}", encoding="utf-8") as file:
  41.                 for words in file.read():
  42.                     print(words, end='')
  43.             print()
  44.             print("Конец файла\n>>>")
  45.  
  46.  
  47.  
  48. if __name__ == "__main__":
  49.     user = work_with_files('C:\\files')
  50.     user_choice = input("Пожалуйста напиште что вы хотите "
  51.                         "сделать с файлом(переименовать названией файлов, посмотреть текст файлов,"
  52.                         "переместить в корзину) и нажмите - Enter>>")
  53.     # print(c.rename_file())
  54.     if user_choice == "ппосмотреть текст файлов":
  55.         print(user.watch_text_in_file())
  56.     elif user_choice == "переименовать названией файлов":
  57.         print(user.rename_file())
  58.     elif user_choice == "переместить в корзину":
  59.         print(user.move_in_bucket())
  60.  
Add Comment
Please, Sign In to add comment