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

2024-04-25 22:14:52 发布

您现在位置:Python中文网/ 问答频道 /正文

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())

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


Tags: selftxtreadinformationfilesdescriptionopenrequirements
3条回答

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

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

或相对路径

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

您应该在路径的最后一个“/”中再添加一个“/”,即: open('C:\Python34\book.csv')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)

当我试图打印一本非常大的字典时,也遇到了同样的错误。当我试图只打印字典的钥匙时,一切都很好!

相关问题 更多 >