~ubuntu-branches/ubuntu/saucy/3depict/saucy

« back to all changes in this revision

Viewing changes to docs/samples/externalProg/python-example.py

  • Committer: Package Import Robot
  • Author(s): D Haley
  • Date: 2013-05-17 00:52:39 UTC
  • mfrom: (3.1.4 experimental)
  • Revision ID: package-import@ubuntu.com-20130517005239-7bl4mnhkvrhc2ba6
Tags: 0.0.13-1
Upload to unstable 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
import sys
 
4
import os 
 
5
 
 
6
 
 
7
#Append the contents of one file to another
 
8
def appendFile(sourceFile,targetFile):
 
9
        try : 
 
10
                fileSrc = open(sourceFile,"rb")
 
11
                fileTarget = open(targetFile,"ab")
 
12
        
 
13
                #Extremely inefficient!!
 
14
                byte = fileSrc.read(1)
 
15
                while byte != "" :
 
16
                        fileTarget.write(byte)
 
17
                        byte=fileSrc.read(1)
 
18
 
 
19
        except IOError:
 
20
                return 1
 
21
 
 
22
        return 0
 
23
 
 
24
def main():
 
25
        argv = sys.argv
 
26
        #Name of file that we will dump our results to
 
27
        OUTPUT_POSFILE="output.pos"
 
28
 
 
29
        #Remove any old files from previous runs
 
30
        if os.path.isfile(OUTPUT_POSFILE) :
 
31
                os.remove(OUTPUT_POSFILE)
 
32
 
 
33
 
 
34
        # do nothing if we have no arguments
 
35
        if(len(argv) < 2) :
 
36
                return 0;
 
37
 
 
38
        #Loop over all our inputs, then for .pos files, 
 
39
        # create one big file with all data merged 
 
40
        for i in argv[1:] : 
 
41
                print "given file :" + i
 
42
 
 
43
                fileExt = i[-3:];
 
44
                if fileExt  == "pos" :
 
45
                        if appendFile(i,OUTPUT_POSFILE):
 
46
                                return 1; #Output to file failed, for some reason
 
47
                        else : 
 
48
                                print "appended file to " + OUTPUT_POSFILE
 
49
 
 
50
                else :
 
51
                        #Filename did not end in .pos, lets ignore it.
 
52
                        print "File :" + i + " does not appear to be a pos file";
 
53
 
 
54
 
 
55
        return 0
 
56
 
 
57
if __name__ == "__main__":
 
58
    sys.exit(main())
 
59
 
 
60
 
 
61