# 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()