我已经写了一个代码来gunzip源文件夹中存在的所有文件。但我想包括检查,如果gunzip文件不存在,然后gunzip它移动到下一个文件。Gunzip Python源文件夹中的所有文件
source_dir = "/Users/path"
dest_dir = "/Users/path/Documents/path"
for src_name in glob.glob(os.path.join(source_dir, '*.gz')):
base = os.path.basename(src_name)
dest_name = os.path.join(dest_dir, base[:-3])
with: gzip.open(src_name, 'rb') as infile, open(dest_name, 'wb') as outfile:
try:
for line in infile:
print ("outfile: %s" %outfile)
if not os.path.exists(dest_name):
outfile.write(line)
print("converted: %s" %dest_name)
except EOFError:
print("End of file error occurred.")
except Exception:
print("Some error occurred.")
我已经使用os.path.exist
检查文件是否存在,但它似乎是os.path.exist
不在这里工作了。
对于问题2,使用您的操作系统的计划作业实用程序。无需尝试编程自己的。 – glibdud
第1部分没有问题。第2部分对于堆栈溢出来说过于宽泛。 –
@MadPhysicist,我想添加一个检查,如果gunzip文件不存在,那么只有gunzip它明智的移动到下一个文件。如何检查这个? – rnvs1116