我想上传一个文件(可以是tar,tar.gz,bz2或zip之一),在上传之前,我想检查tar(或任何压缩格式)的内容是否只有文件夹在顶部的名字与变量'name'的名字相同。如何检查档案是否只有一个文件夹在python中?
在我的代码中,我能够上传文件,但我无法检查上述条件。请注意,代码中的getnames()不起作用。我们如何检查顶部有一个给定名称的文件夹?
请注意,文件夹内可能没有文件。这些文件夹可能是空的。
下面是伪代码:
with Archive(fileobj=data['zxc']) as archive:
if archive.is_archive():
count=0
nam=""
for a in (get names of folders at the top):
nam=""
count+=1
if count!=1:
print "error"
elif nam!=name:
print "error2"
else:
print "correct"
什么是utils.archives?它看起来不像一个标准的Python库。它在哪里记录? – Oin
它在这里:https://github.com/django/django/blob/master/django/utils/archive.py – Sam