-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 ?

2
  • 5
    You have to actually call the function: extractFile() Commented Sep 24, 2014 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… Commented Sep 24, 2014 at 19:04

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

3 Comments

This works but how could this work when I want to pass the variables as arguments?
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 or this for more information.
Btw, you don't need semicolons at the end of the line in Python by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.