CMS 3D CMS Logo

copyAndRename.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 from __future__ import print_function
3 import sys,os,subprocess
4 from CommonMethods import *
5 def main():
6  if len(sys.argv) < 3:
7  error = "Usage: copyAndRename fromDir toDir"
8  exit(error)
9  sourceDir = sys.argv[1] + '/'
10  destDir = sys.argv[2] + '/'
11 
12  fileList = ls(sourceDir,".txt")
13  if not os.path.isdir(destDir):
14  error = "WARNING: destination directory doesn't exist! Creating it..."
15  print(error)
16  os.mkdir(destDir)
17  copiedFiles = cp(sourceDir,destDir,fileList)
18 
19  if len(copiedFiles) != len(fileList):
20  error = "ERROR: I couldn't copy all files from castor"
21  exit(error)
22 
23  for fileName in fileList:
24  fullFileName = destDir + fileName
25  runNumber = -1;
26  with open(fullFileName,'r') as file:
27  for line in file:
28  if line.find("Runnumber") != -1:
29  tmpRun = int(line.split(' ')[1])
30  if runNumber != -1 and tmpRun != runNumber:
31  error = "This file (" + fileName + ") contains more than 1 run number! I don't know how to deal with it!"
32  exit(error)
33  runNumber = int(line.split(' ')[1])
34  file.close()
35  newFileName = fileName.replace("1_.txt",str(runNumber)+"_1_.txt")
36  if fileName != newFileName:
37  aCmd = "mv " + destDir + fileName + " " + destDir + newFileName
38  print(aCmd)
39  output = subprocess.getstatusoutput(aCmd)
40  if output[0] != 0:
41  print(output[1])
42  else:
43  print("WARNING couldn't find keyword None in file " + fileName)
44 
45 
46 
47 
48 
49 if __name__ == "__main__":
50  main()
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def ls(path, rec=False)
Definition: eostools.py:349
Definition: main.py:1
#define str(s)
def exit(msg="")