gpt4 book ai didi

python - python新手。为什么我的脚本没有上传文件?

转载 作者:行者123 更新时间:2023-12-03 08:53:17 25 4
gpt4 key购买 nike

我是python的新手,并尝试递归将目录中所有tar文件的gz内容及其内部目录上传到s3。总共大约有1,350,000个tar文件。

我没有空间立即解压缩所有文件,因此我一次只能处理其中一个。

本来我的脚本可以工作,但是一旦遇到错误(tar文件已损坏),我的脚本就会结束。我添加了一堆tryexcept子句来尝试记录并继续解决这些错误,即使我得到如下输出,我的脚本似乎也没有上传文件:

('iter: ', '/srv/nfs/storage/bucket/2015/11/20/KFWS/NWS_NEXRAD_NXL2DP_KFWS_20151120130000_20151120135959.tar')
KFWS20151120_130030_V06.gz
('singlepart', <Bucket: bucket>, '/srv/nfs/storage/bucket/2015/11/20/KFWS/KFWS20151120_130030_V06.gz', '2015/11/20/KFWS/KFWS20151120_130030_V06.gz')
('single_part: ', '2015/11/20/KFWS/KFWS20151120_130030_V06.gz', '/srv/nfs/storage/bucket/2015/11/20/KFWS/KFWS20151120_130030_V06.gz')
KFWS20151120_131000_V06.gz
('iter: ', '/srv/nfs/storage/bucket/2015/11/20/KFWS/NWS_NEXRAD_NXL2DP_KFWS_20151120110000_20151120115959.tar')
KFWS20151120_110630_V06.gz
('singlepart', <Bucket: bucket>, '/srv/nfs/storage/bucket/2015/11/20/KFWS/KFWS20151120_110630_V06.gz', '2015/11/20/KFWS/KFWS20151120_110630_V06.gz')
('single_part: ', '2015/11/20/KFWS/KFWS20151120_110630_V06.gz', '/srv/nfs/storage/bucket/2015/11/20/KFWS/KFWS20151120_110630_V06.gz')
KFWS20151120_111601_V06.gz

它表明它正在进入single_part,对我而言,这意味着至少正在运行singlept函数并尝试上载对象,但从未创建Zimport_errors.list或Znoaa_nexrad_files.list,并且在其中看不到任何新对象桶。

下面的代码:(对不起,它的总粗细。我试图自学python,而且只有几个星期。)

http://pastebin.com/X56FHDaa

这是主要的街区
def singlept(bucket, keyname, local_file):
retries = 0
key_size = 0
local_size = os.path.getsize(local_file)
while retries <= 4 and local_size != key_size:
local_md5 = md5file(local_file=local_file)
print('single_part: ', keyname, local_file)
try:
key = bucket.new_key(keyname)
except Exception:
print('couldn\'t create key: ', keyname)
pass
try:
key.set_contents_from_filename(local_file)
key_size = key.size
with open(successfile, 'ab') as f:
f.write('\n')
f.write(str(local_file + ',' + keyname + ',' + str(key_size) + ',' + str(local_size)))
except Exception:
print('couldn\'t upload file: ', local_file, ' as key: ', keyname)
with open(errorfile, 'ab') as f:
f.write('\n')
f.write(str(local_file + ',' + keyname + ',' + str(key_size) + ',' + str(local_size)))
pass


for dir, subdir, files in os.walk(local_bucket):
s3path = "/".join(str(dir).split('/')[5:])
local_path = str(local_bucket + '/' + s3path)
for fname in files:
if fname.endswith("tar"):
fullpath = local_path + '/' + fname
if (debug):
print('iter: ',fullpath)
with tarfile.open(fullpath, 'r') as tarball:
zips = tarball.getmembers()
try:
tarball.extractall(path=local_path)
except Exception:
with open(errorfile, 'ab') as f:
f.write('\n')
f.write(str(fullpath + ',' + str(os.path.getsize(fullpath))))
continue
for zip in zips:
if (debug):
print(zip.name)
local_file = local_path + '/' + zip.name
keyname = s3path + '/' + zip.name
try:
if zip.size >= 1073741824:
if (debug):
print('multipart',bucket, local_file, keyname)
multipt(bucket, local_file, keyname)
else:
if (debug):
print('singlepart',bucket, local_file, keyname)
singlept(bucket, keyname, local_file)
except Exception:
with open(errorfile, 'ab') as f:
f.write('\n')
f.write(str(local_file + "," + keyname))
continue
if local_file.endswith("gz"):
try:
os.remove(local_file)
except Exception:
print('couldn\'t remove file: ', local_file)
continue

提前非常感谢您的帮助!我正在拔头发!

编辑-直接添加代码并希望修复缩进!它在Atom中看起来正确,但不能正确粘贴。 :-/

最佳答案

except Exception仅捕获Exception类型的异常-请参见https://stackoverflow.com/a/18982726/264822。你应该试试:

   try:
key = bucket.new_key(keyname)
except:
print('couldn\'t create key: ', keyname)
pass

关于python - python新手。为什么我的脚本没有上传文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35496287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com