0

HI i have a small python script which untars a list of files present in a folder.Below is the script.

app = Flask(__name__)
@app.route('/untarJson')

def untarJson():

    outdir="C:\\Users\\esrilka\\Documents\\Tar Files\\Untar"
    inputfilefolder="C:\\Users\\esrilka\\Documents\\Tar Files\\New tar files\\"  
    jsonfiles=[]  
    for filenames in os.listdir(inputfilefolder):
        if filenames.endswith(".tar.gz"):       
            head,tail= os.path.split(filenames)
            basename=os.path.splitext(os.path.splitext(tail)[0])[0]

            t = tarfile.open(os.path.join(inputfilefolder,filenames), 'r')
            for member in t.getmembers():

                if "autodiscovery/report.json" in member.name:
                    with open(os.path.join(outdir,basename + '.json' ), 'wb') as f:

                        f.write(t.extractfile('autodiscovery/report.json').read())





if __name__ == '__main__':
    app.run(debug=True)  

It works fine without flask and in the folder i have four tar files and all 4 files are untarred.

But when i use flask only one file is untarred and the only one file name is displayed.

how can i untar all files inside a folder and also return the name of the files(i.,. only short names and not with full path)

1
  • What you meant by only one file name is displayed ? I dont see any code related to that Commented Oct 16, 2018 at 4:18

1 Answer 1

1

See if the below code works for you, I have changed only little bit to your original code and it works without any issues. All the available tar.gz files are untared and file names gets displayed after request completes,

from flask import Flask, jsonify
import tarfile
import os

app = Flask(__name__)


@app.route('/untarJson')
def untarJson():
    outdir = "C:\\tests\\untared"
    inputfilefolder = "C:\\tests"
    jsonfiles = []
    for filenames in os.listdir(inputfilefolder):
        if filenames.endswith(".tar.gz"):
            head, tail = os.path.split(filenames)
            basename = os.path.splitext(os.path.splitext(tail)[0])[0]

            t = tarfile.open(os.path.join(inputfilefolder, filenames), 'r')
            for member in t.getmembers():

                if "autodiscovery/report.json" in member.name:
                    with open(os.path.join(outdir, basename + '.json'), 'wb') as f:
                        f.write(t.extractfile('autodiscovery/report.json').read())
                        jsonfiles.append(os.path.join(outdir, basename + '.json'))
    return jsonify(jsonfiles), 200


if __name__ == '__main__':
    app.run(debug=True)

After request completed, something like below will be returned (output will be different in your case),
[ "C:\tests\untared\autodiscovery1.json", "C:\tests\untared\autodiscovery2.json", "C:\tests\untared\autodiscovery3.json" ]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.