0

I have isolated a problematic part of my code and run it as 3 lines (see below), yet I still get this weird error where python tells me I used invalid argument, which looks different than the argument passed in. It apparently at random replaces one of my backslashes by double back slash (which shouldn't matter) and alters the name of the file i want opened.

Code:

fl = open("D:\test\sysi\temporary\fe_in.txt","rt")

flstr = fl.read() 

ff = flstr.split("---")[1]

Error:

OSError: [Errno 22] Invalid argument: 'D:\test\\sysi\temporary\x0ce_in.txt'

Anybody has encountered this? Any ideas what could be causing this or what could I try? (I have already deleted and re-created the file in question thinking it could be corrupted, didn't change anything)

Garam Lee
  • 147
  • 12
LuTze
  • 61
  • 7
  • `\f` is a form feed character. `\t` is a tab character. Use forward slashes instead. – khelwood Aug 20 '20 at 10:52
  • In the actual code I obtain part of my path from somewhere else, when the python function obtains path it does it in the form I used. – LuTze Aug 20 '20 at 10:58

2 Answers2

1

I tested it and I needed to use double slash "\\" to use this without your error:

fl = open("D:\\test\\sysi\\temporary\\fe_in.txt","rt")

flstr = fl.read()

ff = flstr.split("---")[1]

I believe it is because a single slash will be used as an escape character.

We do not know if your file is corrupted as you suspected in you question since the argument to "open" already was invalid. Your textfile was never opened before the error.

Now I only get, "[Errno 2] No such file or directory: 'D:\test\sysi\temporary\fe_in.txt'" because I did not place a file like yours in my file system, but if you have it the program should succeed.

PawelBoe
  • 146
  • 7
0

try:

open(r"D:\test\sysi/temporary\fe_in.txt","r")

or

open(r"D:/test/sysi/temporary/fe_in.txt","r")

or

path = r"D:\test\sysi\temporary\fe_in.txt"

surely someone will work

sabcan
  • 407
  • 3
  • 15