嗨,我有一个小的python脚本,它解压缩folder.Below中的文件列表就是脚本。
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) 它工作良好,没有烧瓶,在文件夹中,我有四个tar文件,所有4个文件都是未注册的。
但是当我使用烧瓶时,只有一个文件是未注册的,只有一个文件名是显示出来的。
如何打开文件夹中的所有文件并返回文件的名称(即。(只有短名称而没有完整路径)
发布于 2018-10-16 04:27:17
看看下面的代码是否适用于您,我只对您的原始代码做了一点改动,并且没有任何问题。所有可用的tar.gz文件都将在请求完成后显示文件名,
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)在请求完成后,如下所示的内容将被返回(在您的情况下输出将有所不同),
[ "C:\tests\untared\autodiscovery1.json", "C:\tests\untared\autodiscovery2.json", "C:\tests\untared\autodiscovery3.json" ]
https://stackoverflow.com/questions/52813830
复制相似问题