CMS 3D CMS Logo

copyAndRename.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 from __future__ import print_function
3 import sys,os,commands
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 = commands.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()
eostools.ls
def ls(path, rec=False)
Definition: eostools.py:349
hgcal_conditions::parameters
Definition: HGCConditions.h:86
str
#define str(s)
Definition: TestProcessor.cc:52
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
createfilelist.int
int
Definition: createfilelist.py:10
main
Definition: main.py:1
beamvalidation.exit
def exit(msg="")
Definition: beamvalidation.py:53
copyAndRename.main
def main()
Definition: copyAndRename.py:5