Repository 'split_file_to_collection'
hg clone https://toolshed.g2.bx.psu.edu/repos/bgruening/split_file_to_collection

Changeset 7:0046692724f9 (2020-07-10)
Previous changeset 6:d57735dd27b0 (2020-06-30) Next changeset 8:6cbe2f30c2d7 (2020-07-12)
Commit message:
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/split_file_to_collection commit 6f78214d2c9d7786bfc9d8cbddac7d2613cd314e"
modified:
split_file_to_collection.py
b
diff -r d57735dd27b0 -r 0046692724f9 split_file_to_collection.py
--- a/split_file_to_collection.py Tue Jun 30 15:10:54 2020 -0400
+++ b/split_file_to_collection.py Fri Jul 10 13:41:00 2020 -0400
[
@@ -92,6 +92,12 @@
     return parser
 
 
+def close_files(file_list):
+    # finally, close all files
+    for open_file in file_list:
+        open_file.close()
+
+
 def replace_mapped_chars(pattern):
     """
     handles special escaped characters when coming from galaxy
@@ -160,7 +166,7 @@
         new_file_base = [custom_new_file_name, custom_new_file_ext]
 
     newfiles = [
-        "%s_%06d%s" % (new_file_base[0], count, new_file_base[1])
+        open(os.path.join(out_dir, "%s_%06d%s" % (new_file_base[0], count, new_file_base[1])) , "w")
         for count in range(0, numnew)
     ]
     # bunch o' counters
@@ -195,15 +201,13 @@
                 else:
                     # if is in fresh_files, write header and drop from freshFiles
                     if new_file_counter in fresh_files:
-                        with open(newfiles[new_file_counter], "a+") as handle:
-                            handle.write(header)
+                        newfiles[new_file_counter].write(header)
                         fresh_files.remove(new_file_counter)
                     
                     if sep_at_end:
                         record += line
                     # write record to file
-                    with open(newfiles[new_file_counter], "a+") as handle:
-                        handle.write(record)
+                    newfiles[new_file_counter].write(record)
                     if not sep_at_end:
                         record = line
                     else:
@@ -227,8 +231,10 @@
             else:
                 record += line
         # after loop, write final record to file
-        with open(newfiles[new_file_counter], "a+") as handle:
-            handle.write(record)
+        newfiles[new_file_counter].write(record)
+
+    # close new files
+    close_files(newfiles)
 
 
 def split_by_column(args, in_file, out_dir, top):
@@ -270,17 +276,17 @@
             # write
             if out_file_name not in new_files.keys():
                 # open file (new, so not already open)
-                with open(out_file_path, "a+") as handle:
-                    #current_new_file = open(out_file_path, "w")
-                    handle.write(header)
-                    handle.write(line)
+                current_new_file = open(out_file_path, "w")
+                current_new_file.write(header)
+                current_new_file.write(line)
                 # add to dict
-                new_files[out_file_name] = out_file_path
+                new_files[out_file_name] = current_new_file
             else:
                 # file is already open, so just write to it
-                #new_files[out_file_name].write(line)
-                with open(new_files[out_file_name], "a") as handle:
-                    handle.write(line)
+                new_files[out_file_name].write(line)
+
+    # finally, close all files
+    close_files(new_files.values())
 
 
 if __name__ == "__main__":