简体   繁体   中英

Python error "OSError: [Errno 22] Invalid argument"

I found this function that allows me to remove the last character from a file:

def removelast(myfile):
    with open(myfile, 'rb+') as filehandle:
        filehandle.seek(-1, os.SEEK_END)
        filehandle.truncate()

And it is working when I test it oh a file alone. But when I use it on my program I don't know why it gives me this error "OSError: [Errno 22] Invalid argument".

Here's my program:

import os
def removelast(myfile):
    with open(myfile, 'rb+') as filehandle:
        filehandle.seek(-1, os.SEEK_END)
        filehandle.truncate()
grades = {'Mathématiques': '20.0', 'Français': '20.0', 'ALE': '20.0', 'Espagnole': '20.0', 'Anglais': '20.0', 'SVT': '20.0', 'SPC': '20.0', 'Technologie': '20.0', 'HGPF': '20.0', 'EMC': '20.0', 'EPS': '20.0', 'Art': '20.0'}
coef = {'Mathématiques': '4', 'Français': '4', 'ALE': '1', 'Espagnole': '1', 'Anglais': '2', 'SVT': '2', 'SPC': '2', 'Technologie': '1', 'HGPF': '2', 'EMC': '1', 'EPS': '1', 'Art': '1'}
def foo(l):
    return list(map(float, l))

Complete_name2 = "D:/Users/Jean Paul/OneDrive/Programming/Programs/Prog 2 - Moyenne de Notes/recalculating.py"

f = open(Complete_name2, "w+")
gradev = foo(list(grades.values()))
coefv = foo(list(coef.values()))
r = 0
a = 0
while r < len(foo(list(grades.values()))):
    print("calculation" + str(r) + " = " + "(" + str(gradev[a]) + " * " + str(coefv[a]) + ")", file = f)
    r = r + 1
    a = a + 1

Here is the part that isn't working (I am trying to print at the end of the file "str(eval(<first variables that has been created earlier> + <second> + <third> +...)) ):

print("")
myprint = "average_p1 = eval("
r2 = 0
while r2 < r:
    myprint = myprint + "calculation" + str(r2) + "+"
    r2 = r2 + 1
removelast(Complete_name2)

recalculating.py has been created:

calculation0 = (20.0 * 4.0)
calculation1 = (20.0 * 4.0)
calculation2 = (20.0 * 1.0)
calculation3 = (20.0 * 1.0)
calculation4 = (20.0 * 2.0)
calculation5 = (20.0 * 2.0)
calculation6 = (20.0 * 2.0)
calculation7 = (20.0 * 1.0)
calculation8 = (20.0 * 2.0)
calculation9 = (20.0 * 1.0)
calculation10 = (20.0 * 1.0)
calculation11 = (20.0 * 1.0)

Output:

Traceback (most recent call last):
  File "d:\Users\Jean Paul\OneDrive\Programming\tests python\test3.py", line 29, in <module>
    removelast(Complete_name2)
  File "d:\Users\Jean Paul\OneDrive\Programming\tests python\test3.py", line 4, in removelast
    filehandle.seek(-1, os.SEEK_END)
OSError: [Errno 22] Invalid argument

This error mostly comes when you are entering a wrong or invalid file path. Check your file paths -

Add r before your file path. This might help - The leading r denotes a python raw string. It treats \ as a literal character.

Add r before your file path to make it like - open(r"C:\folder\yourfile.txt","r")

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