[英]Recursively list files within folder and unpack zip ones
I'm trying to recursively list all files inside a folder (including subfolders) and unpack the zip ones. 我正在尝试递归列出文件夹(包括子文件夹)中的所有文件,并解压缩zip文件。 Tha'ts how i'm doing:
这是我的状况:
def scan(real_path):
matches = []
for root, dirnames, filenames in walk(real_path):
for filename in fil(filenames, '*'):
matches.append(path.join(root, filename))
return matches
def unzip(destination, path):
z = ZipFile(path, mode = "r")
for archive in z.namelist():
content = z.read(archive)
archive = open("%s/%s" % (destination, archive), "w")
archive.write(content)
z.close()
def startup_files(starting_over = False):
print "STARTING FILES AGAIN..." if starting_over else \
"STARTING FILES..."
x = scan(folder)
print "Files found in folder:"
print x
for archive in x:
archive = Archive.details(archive)
print "READING:", archive.full_path
if archive.extension == ".ZIP":
unpack_zip(archive)
remove(archive.full_path)
startup_files(True)
Which is causing this bizarre error. 这导致了这个奇怪的错误。 I'm testing the script with 2 files,
1.zip
and 2.ZIP
, in which file i have this file: SFPDB001.GBK
我正在使用2个文件
1.zip
和2.ZIP
测试脚本,在该文件中我具有以下文件: SFPDB001.GBK
The output is: 输出为:
Iteration one: ok 迭代一:好
STARTING FILES...
Files found in folder:
['arp\\_input\\backups\\1.zip', 'arp\\_input\\backups\\2.ZIP']
READING: C:\Users\G1745 IRON\Development\LL\ARP\arp\_input\backups\1.zip
Iteration two: ok 迭代二:好
STARTING FILES AGAIN...
Files found in folder:
['arp\\_input\\backups\\2.ZIP', 'arp\\_input\\backups\\SFPDB001.GBK']
READING: C:\Users\G1745 IRON\Development\LL\ARP\arp\_input\backups\2.ZIP
Iteration three: ERROR: The scan function is finding the right files inside the folder, but the iteration is trying to reach unavailable files. 迭代三:错误:扫描功能正在文件夹内找到正确的文件,但是迭代试图访问不可用的文件。
STARTING FILES AGAIN...
Files found in folder:
['arp\\_input\\backups\\SFPDB001.GBK']
READING: C:\Users\G1745 IRON\Development\LL\ARP\arp\_input\backups\SFPDB001.GBK
READING: C:\Users\G1745 IRON\Development\LL\ARP\arp\_input\backups\SFPDB001.GBK
READING: C:\Users\G1745 IRON\Development\LL\ARP\arp\_input\backups\2.ZIP
C:\Users\G1745 IRON\Development\LL\ARP\arp\_input\backups\2.ZIP
Traceback (most recent call last):
File "arp\__main__.py", line 22, in <module>
main()
File "arp\__main__.py", line 18, in main
ARP(p.parse_args()).run()
File "C:\Users\G1745 IRON\Development\LL\ARP\arp\arp.py", line 26, in run
self.startup_files()
File "C:\Users\G1745 IRON\Development\LL\ARP\arp\arp.py", line 46, in startup_files
self.unpack_zip(archive)
File "C:\Users\G1745 IRON\Development\LL\ARP\arp\arp.py", line 62, in unpack_zip
Zip.unzip(self.folder, archive.full_path)
File "C:\Users\G1745 IRON\Development\LL\ARP\arp\zip.py", line 17, in unzip
z = ZipFile(path, mode = "r")
File "C:\Python25\lib\zipfile.py", line 339, in __init__
self.fp = open(file, modeDict[mode])
IOError: [Errno 2] No such file or directory: 'C:\\Users\\G1745 IRON\\Development\\LL\\ARP\\arp\\_input\\backups\\2.ZIP'
I don't have any idea why this is happening... i have a JS background, and i'm starting with python now, so i don't know if i should do something. 我不知道为什么会这样...我有JS背景,现在我从python开始,所以我不知道我是否应该做些什么。
OBS: I'm using Python v2.5 because its a legacy code. OBS:我使用的是Python v2.5,因为它是旧版代码。
I found the error, i should return the startup_files
when i call it from the looping: 我发现了错误,当我从循环调用它时,我应该返回
startup_files
:
return self.startup_files(True)
That's all. 就这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.