Python 错误:OSError: [Errno 22] 无效参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17392568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:06:02  来源:igfitidea点击:

Python Error: OSError: [Errno 22] Invalid argument

pythonerror-handling

提问by user2510612

I am using an automation framework and I am getting random error after many iterations which is as follows. Can someone help me understand what this could correspond to !!

我正在使用自动化框架,经过多次迭代后出现随机错误,如下所示。有人可以帮我理解这可能对应什么!!

_os.environ['PATH'] = r'C:\DAL;' + _os.environ['PATH']
  File "c:\Python26\lib\os.py", line 420, in __setitem__
    putenv(key, item)
OSError: [Errno 22] Invalid argument

Function Call where it fails:

失败的函数调用:

function:

功能:

    plugin_xml_file_name = plugin_name
else:
    plugin_xml_file_name = plugin_path + "\" + plugin_name
#

_os.environ['PATH'] = r'C:\Intel\DAL;' + _os.environ['PATH']
_os.environ['PATH'] = r'C:\intel\dal;' + _os.environ['PATH']
_os.environ['PATH'] = _lakemore_path + ';' + _os.environ['PATH']
_os.environ['PATH'] = plugin_path + ';' + _os.environ['PATH']

采纳答案by Martijn Pieters

You are creating too long a path and the OS no longer accepts a longer environment variable.

您创建的路径太长,操作系统不再接受更长的环境变量。

Extend the path only once. Test for the presence of the paths you are adding:

只扩展一次路径。测试您添加的路径是否存在:

path = _os.environ['PATH'].split(_os.pathsep)
for extra in (r'C:\Intel\DAL', r'C:\intel\dal', _lakemore_path, plugin_path):
    if extra not in path:
        _os.environ['PATH'] = _os.pathsep.join(extra, _os.environ['PATH'])

This code only adds new elements if not already present.

此代码仅添加尚不存在的新元素。

回答by Hiep Tran

Add one more "/"in the last "/"of path for example: open('C:\Python34\book.csv')to open('C:\Python34\\\book.csv')

更添加一个"/"在过去"/"例如路径: open('C:\Python34\book.csv')open('C:\Python34\\\book.csv')

回答by Anuj Kumar

Avoid including special characters like \a, \b, \t, \n, \r in your directory path. Instead use double slash whenever neccessary. Like \a, \b, \t, \n, \r.

避免在目录路径中包含特殊字符,如 \a、\b、\t、\n、\r。而是在必要时使用双斜线。像\a、\b、\t、\n、\r。

For example FILEPATH: E:\android\new_dir\raw_data\books\Harry.csvshould be written as E:\\android\\new_dir\\raw_data\\books\Harry.csv

例如 FILEPATH:E:\android\new_dir\raw_data\books\Harry.csv应该写成 E:\\android\\new_dir\\raw_data\\books\Harry.csv