Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading