0

I have the following script on Linux that just reads a file and outputs it and it works with no issues. When I take the same script and try to run it on windows I keep getting the following error, not sure why I keep getting this error though

test script

#!/usr/bin/python

f = open("names.txt", "r+")
next = f.read().splitlines()
for i in next:
    print i

error

IOError: [Errno 22] invalid mode ('r+') or filename:
  • Possible duplicate of [Reading lines from text file in python (windows)](http://stackoverflow.com/questions/6229910/reading-lines-from-text-file-in-python-windows) – Sterling Archer Nov 09 '16 at 18:27
  • I can't reproduce this. You need to provide a [mcve]. I suspect the issue is [this](http://stackoverflow.com/questions/15598160/ioerror-errno-22-invalid-mode-r-or-filename-c-python27-test-txt), but as it is, you haven't provided enough details. – Morgan Thrapp Nov 09 '16 at 18:29
  • +, make sure names.txt exists in the same folder your running the script. r+ won't create it. a+ should. – kabanus Nov 09 '16 at 18:31
  • the file does exist in the same directory, I even tried referencing it with the full path and same error, I even gave the permissions of the file everyone full control just to test but same thing, and the code you are looking at is the exact code I am running – Mafioso1823 Nov 09 '16 at 18:35
  • I also tried using the open(r'names.txt', 'r+') and same error – Mafioso1823 Nov 09 '16 at 18:39
  • Odd thing is, the error should end with 'names.txt' (unless you didn't write it?) - it's like it thinks it's an empty string. Try running this in an interactive shell. Can't reproduce either. – kabanus Nov 09 '16 at 18:42
  • ok let me try with interactive – Mafioso1823 Nov 09 '16 at 18:42
  • wow, I figured out what the issue was, thanks for telling me to use the interactive shell, when I did, I just did an os.system("dir") and noticed that the file was actually called names.txt.txt and that's why I kept getting that error – Mafioso1823 Nov 09 '16 at 18:46
  • but yeah its working now, thanks guys – Mafioso1823 Nov 09 '16 at 18:46

0 Answers0