我应该如何让用户在命令行程序中指示目录的文件路径?
How should I get a user to indicate a file path to a directory in a command line program?
我正在开发一个命令行程序,它要求用户为命名空间对象指定多个参数,而我现在不确定如何设计一个允许用户指定文件路径的参数到一个目录,该目录将包含 运行 程序所需的一些文本文件。
因为我试图让这个命令行程序可移植(即它可以在 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
并且仍然可以在多个平台上运行。
您可以使用 os.path.join(input_dir, "file1.txt")
,它将处理所有 OS 特定问题。您已正确初始化解析器,使用输入目录和 os.path.join
也 os.path.exists
打开您的输入文件。
引用参考:https://docs.python.org/2/library/os.path.html
Since different operating systems have different path name conventions, there are several versions of this module in the standard library. The os.path module is always the path module suitable for the operating system Python is running on, and therefore usable for local paths. However, you can also import and use the individual modules if you want to manipulate a path that is always in one of the different formats.
你真的想多了。听起来问题的核心是对 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
。
我正在开发一个命令行程序,它要求用户为命名空间对象指定多个参数,而我现在不确定如何设计一个允许用户指定文件路径的参数到一个目录,该目录将包含 运行 程序所需的一些文本文件。
因为我试图让这个命令行程序可移植(即它可以在 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
并且仍然可以在多个平台上运行。
您可以使用 os.path.join(input_dir, "file1.txt")
,它将处理所有 OS 特定问题。您已正确初始化解析器,使用输入目录和 os.path.join
也 os.path.exists
打开您的输入文件。
引用参考:https://docs.python.org/2/library/os.path.html
Since different operating systems have different path name conventions, there are several versions of this module in the standard library. The os.path module is always the path module suitable for the operating system Python is running on, and therefore usable for local paths. However, you can also import and use the individual modules if you want to manipulate a path that is always in one of the different formats.
你真的想多了。听起来问题的核心是对 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
。