简体   繁体   English

仅使用python脚本中的os.walk()处理特定目录

[英]Only processing specific directories with os.walk() in python script

I have this python script that concatenates a classpath variable. 我有这个串联了classpath变量的python脚本。 It concatenates the directories of all the files that endwith a ".properties" extension as well as a ".jar" extension. 它连接以“ .properties”扩展名和“ .jar”扩展名结尾的所有文件的目录。

However, some ".jar" files as well as ".properties" files are repeated in different directories. 但是,某些“ .jar”文件和“ .properties”文件在不同目录中重复。 So I have decided to only search through 2 folders. 因此,我决定只搜索2个文件夹。 Those named lib and those named properties . 那些命名为lib和那些命名为properties I'm using Python 2.4 我正在使用Python 2.4

#! /usr/bin/env python

import os
import sys
import glob

java_command = "/myapps/java/home/bin/java -classpath "

def any(s):
    for v in s:
        if v:
            return True
    return False


def run(project_dir, main_class, specific_args):
    classpath = []

    for root, dirs, files in os.walk(project_dir):
        classpath.extend(os.path.join(root, f) for f in files if f.endswith('.jar'))
        if any(f.endswith('.properties') for f in files):
            classpath.append(root)

    classpath_augment = ':'.join(classpath)

    args_passed_in = '%s %s %s %s' % (java_command, classpath_augment, main_class, specific_args)
    print args_passed_in
    code = os.system(args_passed_in)
    print code

只需通过生成器comp设置一组对象:

classpath = set(os.path.join(root, f) for f in files if f.endswith('.jar') or f.endswith('.properties'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM