Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-06-13 03:23:09

0001 import os 
0002 import shutil
0003 import helpers
0004 import argparse
0005 import glob
0006 import os
0007 
0008 
0009 
0010 def main():
0011     parser = argparse.ArgumentParser(description="Rename and move output files")
0012     parser.add_argument("-f", "--fileName", action="store", dest="fileName",
0013                           help="Base file name")
0014     parser.add_argument("-s", action="store", dest="source",
0015                           help="Source folder")
0016     parser.add_argument("-t", action="store", dest="target",
0017                           help="Target folder")
0018     args = parser.parse_args()
0019     
0020     
0021     # create target directory
0022     
0023     helpers.ensurePathExists(args.target)
0024     
0025     # remove files in target directory if they exist
0026     
0027     
0028     # before: files have the naming scheme {baseName}.root and {baseName}00X.root
0029     # after: files have the nameing scheme {baseName}_X.root and are in the target folder
0030     
0031     files = glob.glob("{}/{}*.root".format(args.source, args.fileName))
0032     
0033     for file in files:
0034         if file == "{}/{}.root".format(args.source, args.fileName):
0035             shutil.move(file, "{}/{}_1.root".format(args.target, args.fileName))
0036         else:
0037             path, fn = os.path.split(file)
0038             num = int(fn[len(args.fileName):-5])+1 # add +1 as _1 is already occupied by the first file, so 001 becomes _2 etc
0039             shutil.move(file, "{}/{}_{}.root".format(args.target, args.fileName, num))
0040     
0041 
0042 if __name__ == "__main__":
0043     main()