读取文件时出现错误“ OSError:[Errno 22]无效参数”

时间:2020-10-31 14:39:32

标签: python text-files

我正在研究一个可以使用文本文件编码和解码输入的项目。在它解码的部分会出现错误,我不知道为什么。如果您使用google搜索,则会在路径出问题的地方得到结果。但是我没有路。我能做什么?错误在第62行。这是整个代码。那是循环中的第二行,“而我

    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)

1 个答案:

答案 0 :(得分:0)

如果此txt文件的路径在当前位置,则它必须可以工作,但如果不是,则应找到完整路径并将其存储在变量中。像path =“ C:// Desktop //等等