简体   繁体   中英

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. That's the second line in the loop 'while i < lengte + 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 . Like path= “C://Desktop// and more

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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