0

我正在尝试创建一个脚本来遍历特定文件夹的文件,然后根据标准将文件移动到特定文件夹。我可以用我想要的标准编写它,但如果我想改变一个标准而不是更改实际脚本,而不是更改文本文件。

示例代码:

from os import listdir
from os.path import isfile, join

while True:
   files = [item for item in listdir(target_folder) if isfile(join(target_folder,item ))]
   for each_file in files:
       if each_file.endswith('.exe'):
           print 'File Found.

我真正想做的是 Belvedere http://lifehacker.com/341950/belvedere-automates-your-self+cleaning-pc的替代品。我已经创建了大部分的 gui,但我不清楚其余必要的代码。我试图阅读 Belvedere 的源代码,但我很困惑。

4

1 回答 1

0

我不是 100% 清楚你的意思,但你可以在你的 if 语句上定义一个单参数函数,然后如果你需要的话,以后再改变它,即:

from os import listdir,
from os.path import isfile, join

def matching_file(path):
    return path.endswith(".exe")

while True:
    files = [item for item in listdir(target_folder) if isfile(join(target_folder, item))]
    for each_file in files:
        if matching_file(each_file):
            print "File found."
        if "PYTHON" in each_file:
            matching_file = lambda x: x.endswith(".py")

这只是一个愚蠢的例子,但您可以用任何东西代替matching_file,从而能够即时更改您的标准。

于 2013-05-18T01:29:52.760 回答