合并文件和目录的选择选项 [英] Combining file and directory selection options

查看:169
本文介绍了合并文件和目录的选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想的两个参数的功能结合起来的。目前-f可以用于指定单个文件或野生卡,-d可以指定一个目录。我想-f来处理其目前的功能还是目录。

下面是当前选项语句:

  parser.add_option(' -  D','--directory',
        行动='店',DEST =目录,
        默认为无,帮助='指定目录)
parser.add_option(' - F','--file',
        行动='店',DEST ='名',
        默认为无,帮助='指定的文件或通配符)
如果len(sys.argv中)== 1:
    parser.print_help()
    sys.exit()
(选项,参数)= parser.parse_args()

下面是在功能逻辑,可以把这些结合起来?

  filenames_or_wildcards = []#删除下一行,如果你不想让到不带-f -d运行脚本
#选项,但与论据
filenames_or_wildcards = ARGS#搭在命令行中通过了所有的文件名#如果-f指定添加它们(在当前工作目录)
如果options.filename不是无:
    filenames_or_wildcards.append(options.filename)#如果-d指定或者什么都没有从目录添加文件
如果options.directory不是无:
    filenames_or_wildcards.append(os.path.join(options.directory,*))#现在展开的所有通配符
#glob.glob将一个文件名或通配符在所有匹配的列表
#不存在的文件名会被丢弃
all_files = []
在filenames_or_wildcards filename_or_wildcard:
    all_files.extend(glob.glob(filename_or_wildcard))


解决方案

您可以传递通配符,目录和文件的列表此功能。然而,你的shell会扩大你的通配符,如果你不把在命令行引号之间他们。

 导入OS
进口水珠结合高清(参数):
    all_files = []
    在参数ARG:
        如果*在Arg或?在ARG:
            #包含通配符
            all_files.extend(glob.glob(ARG))
        ELIF os.path.isdir(ARG):
            #是一本字典
            all_files.extend(glob.glob(os.path.join(阿根廷,'*')))
        ELIF os.path.exists(ARG):
            #是一个文件
            all_files.append(ARG)
        其他:
            #无效?
            打印'%s的无效'%ARG
    返回all_files

I would like to combine the functionality of two arguments to one. Currently -f may be used to specify a single file or wild-card, -d may specify a directory. I would like -f to handle its currently functionality or a directory.

Here's the current options statements:

parser.add_option('-d', '--directory',
        action='store', dest='directory',
        default=None, help='specify directory')
parser.add_option('-f', '--file',
        action='store', dest='filename',
        default=None, help='specify file or wildcard')
if len(sys.argv) == 1:
    parser.print_help()
    sys.exit()
(options, args) = parser.parse_args()

Here is the logic in the functionality, can these be combined?

filenames_or_wildcards = []

# remove next line if you do not want allow to run the script without the -f -d
# option, but with arguments
filenames_or_wildcards = args # take all filenames passed in the command line

# if -f was specified add them (in current working directory)
if options.filename is not None:
    filenames_or_wildcards.append(options.filename)

# if -d was specified or nothing at all add files from dir
if options.directory is not None:
    filenames_or_wildcards.append( os.path.join(options.directory, "*") )

# Now expand all wildcards
# glob.glob transforms a filename or a wildcard in a list of all matches
# a non existing filename will be 'dropped'
all_files = []
for filename_or_wildcard in filenames_or_wildcards:
    all_files.extend( glob.glob(filename_or_wildcard) )

解决方案

You can pass a list of wildcards, directories and files to this function. However, your shell will expand your wildcards if you do not put them between quotes in the command line.

import os
import glob

def combine(arguments):
    all_files = []
    for arg in arguments:
        if '*' in arg or '?' in arg:
            # contains a wildcard character
            all_files.extend(glob.glob(arg))
        elif os.path.isdir(arg):
            # is a dictionary
            all_files.extend(glob.glob(os.path.join(arg, '*')))
        elif os.path.exists(arg):
            # is a file
            all_files.append(arg)
        else:
            # invalid?
            print '%s invalid' % arg
    return all_files

这篇关于合并文件和目录的选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆