【发布时间】:2013-07-07 15:32:11
【问题描述】:
我正在编写样板来处理稍后将传递给另一个函数的命令行参数。这个其他函数将处理所有目录创建(如果需要)。因此,我的 bp 只需要检查输入字符串是否可能是有效目录,或有效文件,或(其他一些东西)。 即它需要区分诸如“c:/users/username/”和“c:/users/username/img.jpg”之类的东西
def check_names(infile):
#this will not work, because infile might not exist yet
import os
if os.path.isdir(infile):
<do stuff>
elif os.path.isfile(infile):
<do stuff>
...
标准库似乎没有提供任何解决方案,但理想的情况是:
def check_names(infile):
if os.path.has_valid_dir_syntax(infile):
<do stuff>
elif os.path.has_valid_file_syntax(infile):
<do stuff>
...
在输入时考虑问题后,我无法理解一种方法来检查(仅基于语法)字符串是否包含文件扩展名和尾部斜杠以外的文件或目录(两者都可能不在那里)。可能刚刚回答了我自己的问题,但如果有人对我的胡言乱语有想法,请发表。谢谢!
【问题讨论】: