1

I'm running this code in Sublime Text 3 with python 3.6 installed. The Error I get:

Traceback (most recent call last):
  File "C:\SublimeCode\Creating list from txt.pyw", line 3, in <module>
    List = open('C:\SublimeCode\Employee\EmployeeList.txt').readlines()
FileNotFoundError: [Errno 2] No such file or directory: 
'C:\\SublimeCode\\Employee\\EmployeeList.txt'

I have all these folders as listed in the correct places but yet I get the error. Code:

List = open('C:\SublimeCode\Employee\EmployeeList.txt').readlines()

print(List)
Rubyink
  • 31
  • 2

1 Answers1

0

First using \ is a string literal you must use a \\ to escape this property (or you can use / instead) and you should also set a property as well as read the file before attempting to manipulate the contents.

You can set the property using r (in this case)

list_ = open('C:\\SublimeCode\\Employee\\EmployeeList.txt', 'r').readlines()
print(list_)

You should not use capitals in variables only to set constants. Since list is a function you can escape this property by adding a _ after it see pep8 and as described in this post.

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • This hasn't solved the problem. I ran the code provided and I'm still getting the same error. Any other reasons why this might be happening? – Rubyink Feb 08 '18 at 19:46
  • @Rubyink Duh I missed out the most important point. Could you please run it again I have edited my code? – Xantium Feb 08 '18 at 19:47
  • That worked. It found the file but hasn't been able to read it? File "C:\SublimeCode\Creating list from txt.pyw", line 4, in list_.readlines() AttributeError: 'str' object has no attribute 'readlines' – Rubyink Feb 08 '18 at 19:57
  • @Rubyink Yes I've found the problem. `readlines()` *does* read the file. So using `read()` is totally unnecessary. Both mistakes were from my end *sorry*. – Xantium Feb 08 '18 at 20:02