简体   繁体   中英

OSError: [Errno 22] Invalid argument with a \r

I use JSON to make.txt files but when I make them with this

with open("Saves/" + setname + ".txt", "w") as savefile:
    json.dump(data, savefile)

... I get this:

Traceback (most recent call last):
  File "c:\Users\creep\OneDrive\Bureaublad\Oyunlar\Retraich\main.py", line 647, in <module>
    new_save()
  File "c:\Users\creep\OneDrive\Bureaublad\Oyunlar\Retraich\main.py", line 448, in new_save
    with open("Saves/" + setname + ".txt", "w") as savefile:
OSError: [Errno 22] Invalid argument: 'Saves/hello\r.txt'

I tried setting them in a function. No code adds that \r in the file name as well (and I think that that is where the error arises from). I tried setting them in a define function and still not working. And the setname variable is Hello

setname is initialized to "hello\r" . A carriage return ( \r ) is not a valid character in Windows file paths. You can avoid this by trimming invalid codepoints from your string:

setname = setname.strip("<>:\"\\/|?*\r\n")

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