

# coding: utf-8

# In[1]:


import re
import os
import threading

def main(file_root, result_root):
    for e_txt in os.listdir(file_root):
        try:        
            f=open(file_root+e_txt, 'rU')
            content=f.read()
            f.close()          
            
            content=re.sub(r'[^A-Za-z]', ' ', content)
            content=re.sub(r'\s{2,}', ' ', content)
            content=content.strip().lower()
            
            f1=open(result_root + e_txt, 'wb')            
            f1.write(content)
            f1.close()           
        except Exception as e:
            print e
            print 'handle file: %s error!' % e_txt

if __name__=='__main__':
    # Edit Area
    # ===================================================================
    # the file_path should contain txt files
    file_path   = r'/usr/yyy/wk5/demo_68_txt/'
    result_path = r'/usr/yyy/wk5/txt_filtered/'
    # ===================================================================
    
    if not os.path.exists(result_path):
        os.mkdir(result_path)
    
    main(file_path, result_path)
    
    print '---------------------------Done---------------------------'
    
    

    
    
    
    


# In[ ]:





@@ -70,20 +70,20 @@
 ef main(
-file
+root
 _root, r
@@ -123,20 +123,20 @@
 listdir(
-file
+root
 _root):%0A
@@ -175,20 +175,20 @@
  f=open(
-file
+root
 _root+e_
@@ -787,16 +787,16 @@
   # 
-the file
+The root
 _pat
@@ -825,17 +825,18 @@
 iles
+.
 %0A    
-file
+root
 _pat
@@ -1081,20 +1081,20 @@
    main(
-file
+root
 _path, r

Update 1txt_filter.py