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 | |
---|
13 | class UIOFiles: |
---|
14 | 'class to manipulate files' |
---|
15 | def __init__(self,suffix): |
---|
16 | # type of files to manipulate such as ['.grib', 'grb', 'grib1', 'grib2', 'grb1','grb2'] |
---|
17 | self.suffix=suffix |
---|
18 | |
---|
19 | def listFiles(self,pathname): |
---|
20 | ''' list files (suffix previously given) within this directory. ''' |
---|
21 | # Get the absolute path of the pathname parameter |
---|
22 | pathname = os.path.abspath(pathname) |
---|
23 | |
---|
24 | # Get a list of files in pathname |
---|
25 | filesInCurDir = os.listdir(pathname) |
---|
26 | self.counter = 0 |
---|
27 | self.files = [] |
---|
28 | # Traverse through all files |
---|
29 | for file in filesInCurDir: |
---|
30 | curFile = os.path.join(pathname, file) |
---|
31 | |
---|
32 | # Check if it's a normal file or directory |
---|
33 | if os.path.isfile(curFile): |
---|
34 | # Get the file extension |
---|
35 | fileNoExt,curFileExtension = os.path.splitext(curFile) |
---|
36 | # Check if the file has an extension of typical video files |
---|
37 | if curFileExtension in self.suffix: |
---|
38 | # We have got a file file! Increment the counter |
---|
39 | self.counter += 1 |
---|
40 | # add this filename in the list |
---|
41 | self.files.append(curFile) |
---|
42 | |
---|
43 | else: |
---|
44 | # We got a directory, enter into it for further processing |
---|
45 | self.listFiles(curFile) |
---|