我应该如何让用户在命令行程序中指明目录的文件路径?

时间:2017-07-18 14:39:32

标签: python-2.7 argparse

我正在开发一个命令行程序,要求用户指定Namespace对象的一些参数,我现在不确定如何设计一个允许用户指示文件路径的参数目录,其中包含程序运行所需的一些文本文件。

因为我试图让这个命令行程序可移植(即它可以在Linux,Mac和Windows上运行),我不知道应该如何设计参数以允许来自不同平台的用户指出他们的相关文件路径,因为Windows中的文件路径必须使用反斜杠,而不是Mac或Linux中的文件路径。

我应该如何设计下面设置的参数?

import argparse
from os import path

Parser = argparse.ArgumentParser(prog= "Parser")
Parser.description= "This program helps with stuff blah blah blah"

Parser.add_argument("-input_dir", help= "Please indicate the file path that points to the location of the input text files.")

Parser.add_argument("-input_text", help= "Please indicate the specific text file you want to analyze.")

args= Parser.parse_args()

file_path= args.input_dir
text_file= path.join(file_path, args.input_text)

附录

我应该注意到,我想要完成的是允许用户使用其他参数在命令行中键入他们的文件路径,以便他们的输入看起来像:

python testcript.py -input_dir ~/Documents/Input_Directory -input_text somefile.txt

并且仍可在多个平台上运行。

2 个答案:

答案 0 :(得分:2)

严重过度思考这个问题。听起来问题的核心是os.path 实际跨平台工作缺乏信心。让我向你保证,确实如此!这个模块已经存在了27年(!),在Windows和UNIX上都没有问题。

而不是假设某些东西不会起作用,然后先发制人地寻找变通方法(可能涉及重新发明轮子),而应该测试它并查看它是否有效。在未来,这将避免担心非问题。

在其中一条评论中,您表示担心能够以跨平台方式将文件名与目录分开,这就是您希望用户独立提供这些文件的原因。这是不正确的想法。理解这种方法的理智方法是从用户那里获取完整路径并自行破解(如果没有必要,用户讨厌做额外的工作)。

import argparse
import os.path

Parser = argparse.ArgumentParser(prog= "Parser")
Parser.description= "This program helps with stuff blah blah blah"

# Added required=True since I imagine you need this path...
Parser.add_argument("-text-file", help= "Please indicate the path to the text file.", required=True)

args= Parser.parse_args()

def make_path_sane(p):
    """Function to uniformly return a real, absolute filesystem path."""
    # ~/directory -> /home/user/directory
    p = os.path.expanduser(p)
    # A/.//B -> A/B
    p = os.path.normpath(p)
    # Resolve symbolic links
    p = os.path.realpath(p)
    # Ensure path is absolute
    p = os.path.abspath(p)
    return p

text_file = make_path_sane(args.text_file)
input_dir = os.path.dirname(text_file)
input_text = os.path.basename(text_file)
print text_file
print input_dir
print input_text

通过此设置,用户可以选择以提供完整路径,相对路径,或仅提供文件名本身,它将始终有效!在任何文件系统上!

% python2 parse.py -text-file ~/directory/file.txt
/home/smmorton/directory/file.txt
/home/smmorton/directory
file.txt

% python2 parse.py -text-file /home/smmorton/directory/file.txt
/home/smmorton/directory/file.txt
/home/smmorton/directory
file.txt

% python2 parse.py -text-file directory/file.txt
/home/smmorton/directory/file.txt
/home/smmorton/directory
file.txt

% cd ~/directory
% python2 parse.py -text-file file.txt
/home/smmorton/directory/file.txt
/home/smmorton/directory
file.txt

% python2 parse.py -text-file ../directory/./file.txt
/home/smmorton/directory/file.txt
/home/smmorton/directory
file.txt

如果您使用的是Python> = 3.4,我建议您使用pathlib代替os.path

答案 1 :(得分:0)

您可以使用os.path.join(input_dir, "file1.txt"),它将处理所有操作系统特定问题。您已正确初始化解析器,使用输入目录和os.path.join os.path.exists来打开输入文件。

引用参考:https://docs.python.org/2/library/os.path.html

  

由于不同的操作系统具有不同的路径名约定,因此标准库中有此模块的多个版本。 os.path模块始终是适用于Python运行的操作系统的路径模块,因此可用于本地路径。但是,如果要操作始终采用不同格式之一的路径,也可以导入和使用各个模块。