在 Python 中使用 open() 时 OSError [Errno 22] 无效参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25584124/
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-18 20:24:45  来源:igfitidea点击:

OSError [Errno 22] invalid argument when use open() in Python

python

提问by eugene

def choose_option(self):
        if self.option_picker.currentRow() == 0:
            description = open(":/description_files/program_description.txt","r")
            self.information_shower.setText(description.read())
        elif self.option_picker.currentRow() == 1:
            requirements = open(":/description_files/requirements_for_client_data.txt", "r")
            self.information_shower.setText(requirements.read())
        elif self.option_picker.currentRow() == 2:
            menus = open(":/description_files/menus.txt", "r")
            self.information_shower.setText(menus.read())

I am using resource files and something is going wrong when i am using it as argument in open function, but when i am using it for loading of pictures and icons everything is fine.

我正在使用资源文件,当我在 open 函数中使用它作为参数时出现问题,但是当我使用它加载图片和图标时,一切都很好。

回答by Cory Kramer

That is not a valid file path. You must either use a full path

这不是有效的文件路径。您必须使用完整路径

open(r"C:\description_files\program_description.txt","r")

Or a relative path

或者相对路径

open("program_description.txt","r")

回答by Hiep Tran

you should add one more "/" in the last "/" of path, that is: open('C:\Python34\book.csv')to open('C:\Python34\\book.csv'). For example:

您应该在路径的最后一个“/”中再添加一个“/”,即: open('C:\Python34\book.csv')to open('C:\Python34\\book.csv')。例如:

import csv
with open('C:\Python34\book.csv', newline='') as csvfile:
    spamreader = csv.reader(csvfile, delimiter='', quotechar='|')
    for row in spamreader:
        print(row)

回答by Abhijeet

In Windows-Pycharm: If File Location|Path contains any string like \tthen need to escape that with additional \like \\t

在Windows的Pycharm:如果文件位置|路径中包含任何字符串如\t再需要逃避与另外\\\t

回答by P113305A009D8M

Just replace with "/" for file path :

只需用“/”替换文件路径:

   open("description_files/program_description.txt","r")

回答by duhaime

I received the same error when trying to print an absolutely enormous dictionary. When I attempted to print just the keys of the dictionary, all was well!

在尝试打印绝对巨大的字典时,我收到了同样的错误。当我试图只打印字典的键时,一切都很好!

回答by wolfog

I also ran into this fault when I used open(file_path). My reason for this fault was that my file_pathhad a special character like "?"or "<".

我在使用的时候也遇到了这个故障open(file_path)。我这个错误的原因是我file_path有一个特殊的字符,比如"?"or "<"

回答by Prostak

for folder, subs, files in os.walk(unicode(docs_dir, 'utf-8')):
    for filename in files:
        if not filename.startswith('.'):
            file_path = os.path.join(folder, filename)

回答by Mohsen Hrt

In my case,the problem exists beacause I have not set permission for drive "C:\" and when I change my path to other drive like "F:\" my problem resolved.

就我而言,问题存在是因为我没有为驱动器“C:\”设置权限,当我将路径更改为“F:\”等其他驱动器时,我的问题解决了。

回答by Lio Ak

I had the same problem It happens because files can't contain special characters like ":", "?", ">" and etc. You should replace these files by using replace() function:

我遇到了同样的问题 这是因为文件不能包含特殊字符,如“:”、“?”、“>”等。您应该使用 replace() 函数替换这些文件:

filename = filename.replace("special character to replace", "-")

回答by I Iv

import pandas as pd
df = pd.read_excel ('C:/Users/yourlogin/new folder/file.xlsx')
print (df)