Last active
August 29, 2015 13:57
-
-
Save strathmeyer/9630642 to your computer and use it in GitHub Desktop.
Collects .cbf files from multiple subdirectories, copies them to a common output directory, modifies the filename of each and also modifies a line inside the file to match the new filename.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
import os | |
import re | |
# Create the output directory if it doesn't exist | |
output_dir = './output/' | |
if not os.path.exists(output_dir): | |
os.makedirs(output_dir) | |
# Pull in all the input file names. The "**"" means "this directory and any | |
# sub-directories up to any depth" | |
input_files = glob.glob('./**/*.cbf') | |
# Loop through the files. | |
# index will get 0, 1, 2... input_file will get the filenames | |
for index, input_file in enumerate(input_files): | |
# Create our output slug by padding the index with zeros | |
output_slug = 'x17_1_{:05d}'.format(index + 1) | |
output_file = output_dir + output_slug + '.cbf' | |
print "Processing", input_file, 'into', output_file | |
# Open up the input file and the output file | |
with open(input_file, "rt") as fin: | |
with open(output_file, "wt") as fout: | |
# Go through each line of the input file | |
for line in fin: | |
# Try to replace the data line with our new filename in each line | |
line = re.sub( | |
r"data_x\d\d_\d_\d\d\d\d\d", # input regex (what to replace) | |
"data_" + output_slug, # output (what to replace it with) | |
line # data to act upon | |
) | |
# Write the modified line to the output | |
fout.write(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment