python 文件Joiner

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 文件Joiner相关的知识,希望对你有一定的参考价值。

# File Joiner v2

import sys
from itertools import islice
import inspect
import datetime
import time 
import os

# +++your code here+++
# Define print_words(file_name) and print_top(file_name) functions.
def file_joiner(folder_name):
  
  print folder_name

  filenames = []

  for file_name in os.listdir(folder_name):

    full_file_name = folder_name + "/" + file_name

    print "\nCHECKING FILE NAME : " + file_name

    if full_file_name.find(".") > -1:
      
      if full_file_name.find('.txt') == -1:

        print "CHECKING FILE NAME : NOT OKAY (SKIPPING) : " + file_name

        continue    

    print "CHECKING FILE NAME : OKAY : " + file_name
    print "CHECKING FILE NAME : ADDING TO JOIN LIST : " + file_name
    filenames.append(full_file_name)

  # Write File  
  print "JOINING FILES...please wait..."

  # set file name
  currenttime = datetime.datetime.fromtimestamp(int(time.time())).strftime('%Y-%m-%d_%H_%M_%S')  

  # csv_location_and_name = localfolder + "/" + csvfile_name+".csv"
  new_file_name = folder_name + "/" + "joined_file" + "_" + currenttime + ".txt"  


  with open(new_file_name, 'w') as outfile:
    for fname in filenames:
        with open(fname) as infile:
            for line in infile:
                outfile.write(line)
  
  print "\nNEW FILE CREATED IN YOUR FOLDER"
  print "\nNEW FILE NAME = " + new_file_name + "\n"

###

# This basic command line argument parsing code is provided and
# calls the print_words() and print_top() functions which you must define.
def main():
  
  if len(sys.argv) != 2:
    print '\nTo open, write: "python ./file_joiner.py ---./FOLDER_OF_WHERE_FILES_ARE---\n'
    print 'All files in folder must have either ".txt" or no extension, e.g. "file_name.txt" or "file_name"\n'
    print 'For example, "python /Users/me/Log\ Watcher\ Parser/file_joiner.py /Users/me/myfiles/\n'
    sys.exit(1)

  folder_name = sys.argv[1]  
  
  if folder_name:
    file_joiner(folder_name)  
  
if __name__ == '__main__':
  main()

以上是关于python 文件Joiner的主要内容,如果未能解决你的问题,请参考以下文章

Joiner的用法

Guava Joiner 拼接字符串

guava字符串处理 Joiner Splitter CharMatcher

guava之Joiner 和 Splitter(转)

Joiner的简单了解

结合 Grapheme Joiner 在 Cocoa、iOS 中不起作用?