简体   繁体   English

读取文件时出现错误“OSError: [Errno 22] Invalid argument”

[英]I get the error "OSError: [Errno 22] Invalid argument" when I read the file

I'm working on a project that can code and decode a input using text files.我正在开发一个可以使用文本文件对输入进行编码和解码的项目。 In the part where it decodes it give an error and I don't know why.在它解码的部分给出一个错误,我不知道为什么。 If you google it, you get results where the problem is in the path.如果你用谷歌搜索,你会得到路径问题所在的结果。 But I don't have a path.但我没有路。 What can I do?我能做什么? The error is in the line 62. This is the whole code.错误在第 62 行。这是整个代码。 That's the second line in the loop 'while i < lengte + 1'.这是循环中的第二行'while i < lente + 1'。 I am dutch so if the names of variables don't always make sense that's why.我是荷兰人,所以如果变量的名称并不总是有意义,那就是原因。 the full error is完整的错误是

    readed = compressed.read(i)
OSError: [Errno 22] Invalid argument'
message = input("what's your input?")
lengte = len(message)

compressed = open('compressed message.txt', 'a')
compressed.truncate(0)
compressed.close()

codeBook = {}

i = 0

text = message[i]
codeBook[text] = 1
eersteLetter = text + " " 
compressed = open('compressed message.txt', 'a')
compressed.write(eersteLetter)
compressed.close()
i += 1

while i < lengte :
    text = message[i]
    while text in codeBook:
        if i < lengte-1: 
            text = text + message[i+1]
        i += 1
if len(text) > 2:
    duplicate = text[0:-2]
    compressedText = str(codeBook[duplicate]) + text[-1]
    compressed = open('compressed message.txt', 'a')
    compressed.write(compressedText)
    codeBook[text] = len(codeBook)+1
    compressed.close()
elif len(text) > 1:
    duplicate = text[0]
    compressedText = str(codeBook[duplicate]) + text[-1]
    compressed.write(compressedText)
    codeBook[text] = len(codeBook)+1
    compressed.close()
else:
    compressedText = text
    compressed = open('compressed message.txt', 'a')
    compressed.write(compressedText)
    codeBook[text] = len(codeBook)+1
    compressed.close
    i += 1

compressed = open('compressed message.txt', 'r')
print(compressed.read())
compressed.close()
print(codeBook)



bookCode = {}
i = 1
compressed = open('compressed message.txt', 'r')
lengte = len(compressed.read())
compressed.close()

while i < lengte+1:
    compressed = open('compressed message.txt', 'r')
    readed = compressed.read(i)
    compressed.close()
    if readed == ' ':
        i += 1
    elif type(readed) == int:
        i += 1
        compressed = open('compressed message.txt', 'r')
        text = text + bookCode[readed] + compressed.read(i)
        bookCode[len(bookCode)+1] = bookCode[readed] + compressed.read(i)
        compressed.close()
        i += 1
    elif type(readed) == str:
        text = text + readed
        bookCode[len(bookCode)+1] = readed
print(' ')
print(text)
print(bookCode)

if the path of this txt file is in the current locations it must work but if not you should find the full path and store it in an variable .如果这个 txt 文件的路径在当前位置,它必须工作,但如果不是,你应该找到完整路径并将其存储在一个变量中。 Like path= “C://Desktop// and more像 path="C://Desktop// 等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM