【问题标题】:I`m getting an error "[Errno 22] Invalid argument" while trying to open the file尝试打开文件时出现错误“[Errno 22] Invalid argument”
【发布时间】:2016-11-03 04:55:09
【问题描述】:

我正在尝试编写一个接收文件并返回列表的函数。但它不起作用。相反,它说:

'IOError: [Errno 22] 无效参数:'C:\Python32\x07ssignment3\wordlist.txt''

当我尝试运行模块时

words_file_name = 'C:\Python32\assignment3\wordlist.txt'
words_file = open(words_file_name, 'r')

def read_words(words_file):
    words_list = words_file.readlines()
    return words_list

【问题讨论】:

    标签: python function readlines invalid-argument


    【解决方案1】:

    您可以通过将\assignement 中的\a 标记为\\a 来避免这种情况。每当您在目录路径中遇到 \b、\n、\r、\t 等时,请用双斜杠替换它们。

    【讨论】:

      【解决方案2】:

      转义反斜杠或使用原始字符串文字。否则,\a 将变成\x07,这是转义字符\a 的十六进制表示。我建议为此使用原始字符串,这样您就不必处理额外的反斜杠。

      words_file_name = r'C:\Python32\assignment3\wordlist.txt'
                        ^
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-26
        • 2018-06-15
        • 1970-01-01
        • 2020-02-15
        • 2019-08-07
        • 2022-11-04
        • 2020-04-07
        • 2018-12-02
        相关资源
        最近更新 更多