-3

I am new to Python and I'm working with some tar files. The following example works:

#!/usr/bin/python
import os, readline, tarfile, scipy.io, numpy as np, sys
year = 2012;
month = 12;
day = 10;
RS = 9;
hour = 00;
minute = 05;
seconds = 00;
UTC = 1355094300;
anArchive = '/Users/user/data/20121210.zip';
tar = tarfile.open(anArchive);
dynamicPath = './%4d%2d%2d/RS%02d/%02d%02d%02d_%10d/all.txt' %(year, month, day, RS, hour,minute, seconds, UTC);
print(dynamicPath);
memb = tar.getmember(dynamicPath);
file = tar.extractfile(memb.name);
print('loading file with measurements...\n');
contents = file.read();
destinationFile = open("extractedFile.txt", "w");
destinationFile.write(contents);

which gets a file from a tar, extracts it and writes it in a new file.

Now I want to define a function that does the exact same thing:

#!/usr/bin/python
import os, readline, tarfile, scipy.io, numpy as np, sys
def extractFile():
    year = 2012;
    month = 12;
    day = 10;
    RS = 9;
    hour = 00;
    minute = 05;
    seconds = 00;
    UTC = 1355094300;
    anArchive = "/Users/user/data/20121210.zip";
    tar = tarfile.open(anArchive);
    dynamicPath = "./%4d%2d%2d/LOSS_RS%02d/%02d%02d%02d_%10d/all.txt" %(year, month, day, RS, hour,minute, seconds, UTC);
    print(dynamicPath);
    #memb = tar.getmember("./20121210/RS09/004501_1355096701/all.txt");
    memb = tar.getmember(dynamicPath);
    file = tar.extractfile(memb.name);
    print('loading file with measurements...\n');
    contents = file.read();
    destinationFile = open("extractedFile.txt", "w");
    destinationFile.write(contents);
    return

After I save it and make sure that is executable, I execute it from the terminal checking also for Indentation errors:

python -t extractFile.py

and the result is nothing. No error, the execution "finishes" but with no result, like if I had executed empty code.

Any ideas why the same exact code doesn't work when used as a function ?

pedromendessk
  • 3,538
  • 2
  • 24
  • 36
  • 5
    You have to actually call the function: `extractFile()` – mdurant Sep 24 '14 at 19:01
  • 2
    You might want to consider `if __name__ == '__main__':` `extractFile()`. That means that you can use `extractFile` in other programs that import this module, but also use it as a script. But if you don't need that, you don't have to learn about it yet… – abarnert Sep 24 '14 at 19:04

1 Answers1

4

You need to call the function for it to be executed - add this line to the end of your file:

extractFile()

I.e. the whole code should be:

#!/usr/bin/python
import os, readline, tarfile, scipy.io, numpy as np, sys
def extractFile():
    year = 2012;
    month = 12;
    day = 10;
    RS = 9;
    hour = 00;
    minute = 05;
    seconds = 00;
    UTC = 1355094300;
    anArchive = "/Users/user/data/20121210.zip";
    tar = tarfile.open(anArchive);
    dynamicPath = "./%4d%2d%2d/LOSS_RS%02d/%02d%02d%02d_%10d/all.txt" %(year, month, day, RS, hour,minute, seconds, UTC);
    print(dynamicPath);
    #memb = tar.getmember("./20121210/RS09/004501_1355096701/all.txt");
    memb = tar.getmember(dynamicPath);
    file = tar.extractfile(memb.name);
    print('loading file with measurements...\n');
    contents = file.read();
    destinationFile = open("extractedFile.txt", "w");
    destinationFile.write(contents);
    return
extractFile()
levant pied
  • 3,886
  • 5
  • 37
  • 56
  • This works but how could this work when I want to pass the variables as arguments? –  Sep 25 '14 at 14:05
  • Say you want to pass a year parameter, instead of hard-coding it with `year = 2012`. Instead of `def extractFile()`, you'd put `def extractFile(year)` and when calling you'd call it as `extractFile(2012)`. If you want command line parameters, see e.g. [this](http://www.cyberciti.biz/faq/python-command-line-arguments-argv-example/) or [this](http://stackoverflow.com/questions/9036013/python-command-line-parameters) for more information. – levant pied Sep 25 '14 at 15:29
  • Btw, you don't need semicolons at the end of the line in Python by default. – levant pied Sep 25 '14 at 15:29