Advertisement
nicuf

Delete All Files With Less Than 250 Characters - part.1

Apr 12th, 2022 (edited)
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. ----------------------
  2. EXPLANATION:
  3.  
  4. ENGLISH: https://neculaifantanaru.com/en/python-delete-all-files-with-less-than-250-characters.html
  5.  
  6. ROMANIAN: https://neculaifantanaru.com/python-sterge-toate-fisierele-cu-mai-putin-de-250-de-caractere.html
  7. ----------------------
  8.  
  9.  
  10. import os
  11. import re
  12. import random
  13. import unidecode
  14. import nltk
  15. from nltk import tokenize
  16. # nltk.download('punkt')
  17. import requests
  18. from usp.tree import sitemap_tree_for_homepage
  19.  
  20. def read_text_from_file(file_path):
  21.     """
  22.    Aceasta functie returneaza continutul unui fisier.
  23.    file_path: calea catre fisierul din care vrei sa citesti
  24.    """
  25.     with open(file_path, encoding='utf8') as f:
  26.         text = f.read()
  27.         f.close()
  28.         return text
  29.  
  30. FOLDER_LOCAL = 'd:\\Folder1'
  31.  
  32. counter_sterse = 0
  33. for f in os.listdir(FOLDER_LOCAL):
  34.     if f.endswith('.html') or f.endswith('.htm'):
  35.         filepath = os.path.join(FOLDER_LOCAL, f)
  36.         page_html = read_text_from_file(filepath)
  37.         if len(page_html) < 250:
  38.             os.remove(filepath)
  39.             counter_sterse += 1
  40.             continue
  41.  
  42. print("S-au sters {} fisiere".format(counter_sterse))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement