我正試圖在Visual Studio 2015中運行一個python腳本,並且我想指定一個指向我的arparse函數的路徑,但是不斷收到一個OSError。請參閱更新問題似乎是argparse從命令而不是bash行爲接收值的差異。Windows cmd與bash for sys.argv - Python
無論我指定像這樣
C:\Users\Sayth\Documents\Racing\XML\*xml
或類似這樣的
C:\\Users\\Sayth\Documents\\Racing\\XML\\*xml
我得到一個OSERROR的路徑沒有找到
OSError: Error reading file 'C:\\Users\\Sayth\\Documents\\Racing\\XML\\*xml': failed to load external entity "file:/C://Users//Sayth/Documents//Racing//XML//*xml"
更新 我複製將腳本和XML文件轉換爲測試di教區長。從這裏我已經在Windows上的兩個不同的shell上運行腳本。
在命令CMD
C:\Users\Sayth\Projects
λ python RaceHorse.py XML\*xml
Traceback (most recent call last):
File "RaceHorse.py", line 42, in <module>
tree = lxml.etree.parse(file)
File "lxml.etree.pyx", line 3427, in lxml.etree.parse (src\lxml\lxml.etree.c:79720)
File "parser.pxi", line 1782, in lxml.etree._parseDocument (src\lxml\lxml.etree.c:115914)
File "parser.pxi", line 1808, in lxml.etree._parseDocumentFromURL (src\lxml\lxml.etree.c:116264)
File "parser.pxi", line 1712, in lxml.etree._parseDocFromFile (src\lxml\lxml.etree.c:115152)
File "parser.pxi", line 1115, in lxml.etree._BaseParser._parseDocFromFile (src\lxml\lxml.etree.c:109849)
File "parser.pxi", line 573, in lxml.etree._ParserContext._handleParseResultDoc (src\lxml\lxml.etree.c:103323)
File "parser.pxi", line 683, in lxml.etree._handleParseResult (src\lxml\lxml.etree.c:104977)
File "parser.pxi", line 611, in lxml.etree._raiseParseError (src\lxml\lxml.etree.c:103843)
OSError: Error reading file 'XML\*xml': failed to load external entity "XML/*xml"
當我將其更改爲混帳bash的 它讀取文件,我得到一個錯誤,但它顯示了它的工作。
[email protected] ~/Projects
λ python RaceHorse.py XML/*xml
Traceback (most recent call last):
File "RaceHorse.py", line 50, in <module>
nomination_table.append([race_id] + [nomination.attrib[name] for name in horseattrs])
File "RaceHorse.py", line 50, in <listcomp>
nomination_table.append([race_id] + [nomination.attrib[name] for name in horseattrs])
File "lxml.etree.pyx", line 2452, in lxml.etree._Attrib.__getitem__ (src\lxml\lxml.etree.c:68544)
KeyError: 'race_id'
我有一個簡單argparse功能
parser = argparse.ArgumentParser(description=None)
def GetArgs(parser):
"""Parser function using argparse"""
# parser.add_argument('directory', help='directory use',
# action='store', nargs='*')
parser.add_argument("files", nargs="+")
return parser.parse_args()
fileList = GetArgs(parser)
更新基於註釋2 想實現水珠,使使用windows炮彈。 glob正在返回錯誤,它的對象解析器沒有對象len。
更新的水珠解析器
def GetArgs(parser):
"""Parser function using argparse"""
# parser.add_argument('directory', help='directory use',
# action='store', nargs='*')
parser.add_argument("files", nargs="+")
files = glob.glob(parser.parse_args())
return files
filelist = GetArgs(parser)
返回此錯誤。
TypeError was unhandled by user code
Message: object of type 'Namespace' has no len()
它看起來像是(嘗試)將您指定的路徑更改爲其[文件URI方案](https://en.wikipedia.org/wiki/File_URI_scheme)等效項。嘗試使用'\\ C:\ Users \ Sayth \ Documents \ Racing \ XML \ * xml'。 – martineau
看看'sys.argv';這就是'argparse'必須使用的。 – hpaulj
@martineau沒有工作。系統。 argv在bash上工作不知道這裏有什麼區別 – sayth