[e18f4b5] | 1 | #!/usr/bin/env python |
---|
| 2 | # |
---|
| 3 | # (C) Copyright 2014 UIO. |
---|
| 4 | # |
---|
| 5 | # This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
| 6 | # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
| 7 | # |
---|
| 8 | # Creation: October 2014 - Anne Fouilloux - University of Oslo |
---|
| 9 | # |
---|
| 10 | |
---|
| 11 | import os |
---|
| 12 | import glob |
---|
| 13 | |
---|
| 14 | class UIOFiles: |
---|
| 15 | 'class to manipulate files' |
---|
| 16 | def __init__(self,suffix): |
---|
| 17 | # type of files to manipulate such as ['.grib', 'grb', 'grib1', 'grib2', 'grb1','grb2'] |
---|
| 18 | self.suffix=suffix |
---|
| 19 | |
---|
| 20 | def listFiles(self,pathname,pattern): |
---|
| 21 | ''' list files (suffix previously given) within this directory. ''' |
---|
| 22 | # Get the absolute path of the pathname parameter |
---|
| 23 | pathname = os.path.abspath(pathname) |
---|
| 24 | |
---|
| 25 | # Get a list of files in pathname |
---|
| 26 | filesInCurDir0 = glob.glob(pathname+'/'+pattern) |
---|
| 27 | filesInCurDir=[] |
---|
| 28 | for f in filesInCurDir0: |
---|
| 29 | filesInCurDir.append(f.split('/')[-1]) |
---|
| 30 | self.counter = 0 |
---|
| 31 | self.files = [] |
---|
| 32 | # Traverse through all files |
---|
| 33 | for file in filesInCurDir: |
---|
| 34 | curFile = os.path.join(pathname, file) |
---|
| 35 | |
---|
| 36 | # Check if it's a normal file or directory |
---|
| 37 | if os.path.isfile(curFile): |
---|
| 38 | # Get the file extension |
---|
| 39 | fileNoExt,curFileExtension = os.path.splitext(curFile) |
---|
| 40 | # Check if the file has an extension of typical video files |
---|
| 41 | if curFileExtension in self.suffix: |
---|
| 42 | # We have got a file file! Increment the counter |
---|
| 43 | self.counter += 1 |
---|
| 44 | # add this filename in the list |
---|
| 45 | self.files.append(curFile) |
---|
| 46 | |
---|
| 47 | else: |
---|
| 48 | # We got a directory, enter into it for further processing |
---|
| 49 | self.listFiles(curFile) |
---|
| 50 | |
---|
| 51 | return |
---|