[Python] Python合并文本文件 →→→→→进入此内容的聊天室

来自 , 2019-07-07, 写在 Python, 查看 108 次.
URL http://www.code666.cn/view/70117ee3
  1. # coding gbk  
  2. #  
  3. # author: GreatGhoul  
  4. # email : greatghoul@gmail.com  
  5. # blog  : http://greatghoul.javaeye.com  
  6.  
  7. import sys,os,msvcrt  
  8.  
  9. def join(in_filenames, out_filename):  
  10.     out_file = open(out_filename, 'w+')  
  11.      
  12.     err_files = []  
  13.     for file in in_filenames:  
  14.         try:  
  15.             in_file = open(file, 'r')  
  16.             out_file.write(in_file.read())  
  17.             out_file.write('\n\n')  
  18.             in_file.close()  
  19.         except IOError:  
  20.             print 'error joining', file  
  21.             err_files.append(file)  
  22.     out_file.close()  
  23.     print 'joining completed. %d file(s) missed.' % len(err_files)  
  24.     print 'output file:', out_filename  
  25.     if len(err_files) > 0:  
  26.         print 'missed files:'  
  27.         print '--------------------------------'  
  28.         for file in err_files:  
  29.             print file  
  30.         print '--------------------------------'  
  31.  
  32. if __name__ == '__main__':  
  33.     print 'scanning...'  
  34.     in_filenames = []  
  35.     file_count = 0  
  36.     for file in os.listdir(sys.path[0]):  
  37.         if file.lower().endswith('[all].txt'):  
  38.             os.remove(file)  
  39.         elif file.lower().endswith('.txt'):  
  40.             in_filenames.append(file)  
  41.             file_count = file_count + 1  
  42.     if len(in_filenames) > 0:  
  43.         print '--------------------------------'  
  44.         print '\n'.join(in_filenames)  
  45.         print '--------------------------------'  
  46.         print '%d part(s) in total.' % file_count  
  47.         book_name = raw_input('enter the book name: ')  
  48.         print 'joining...'  
  49.         join(in_filenames, book_name + '[ALL].TXT')  
  50.     else:  
  51.         print 'nothing found.'  
  52.     msvcrt.getch()  
  53. #//python/1190

回复 "Python合并文本文件"

这儿你可以回复上面这条便签

captcha