2016-08-14 32 views
0

我想遍历大于100 kb的指定目录中的文件,并以* .zip结尾。如何使用Python有选择地迭代文件

这是如何以有效的方式完成的?

会经历任何zip文件,但不一定大于100kb的文件;

for i in os.listdir(os.getcwd()): 
    if i.endswith(".zip"): 
     ##print i 
     continue 
    else: 
     continue 

如何在if条件下使用它?即(if i.endswith(".zip") and >100kb)。我怎么能用这个文件作为myOtherPythonScript.py的参数?

+3

http://stackoverflow.com/questions/2104080/how-to-check-file-size-in-python –

+1

'os.path.getsize'会派上用场...... –

回答

1

你可以尝试这样的事情......

for i in os.listdir(os.getcwd()): 
    if i.endswith(".zip"): 
     if os.path.getsize(i) > 10240: 
      print i 
    continue 
else: 
    continue 
+0

怎么会一个在if语句中使用文件或'i'作为带有被调用的python脚本的参数?即在'print i'之后,我将如何使用'i'作为'myOtherPythonScript.py'的参数。 – 3kstc

+0

您需要将此类设置为返回I并在myotherpythonscript.py中导入该类的类 – Lance

0

endswithos.path.getsize是你想要的两个功能。

import os 

file_names = [os.path.join(path, file_name) for file_name in os.listdir(path)] 
for file_name in file_names: 
    if file_name.endswith('zip') and os.path.getsize(file_name) >= 100*1024: 
     pass 
    else: 
     pass