The problem with your code is in the very last line:
print (F3)
F3
was the name of the local variable inside the function. You can't access that from here.
But you can access the same value that was in that variable, because the function returned it, and you stored it in Words
.
So, just do this:
print(Words)
And now, your code works.
That being said, it can be improved.
Most importantly, look at this part:
F3=[]
for word in F2:
F3=F2.lower()
The for word in F2:
actually loops over every character in F2
, because that's how strings work. If you want to go word by word, you need to do something like for word in F2.split():
Meanwhile, inside the loop, you reassign F3
each time through the loop, and never do anything with the previous value, so the whole thing ends up being a very fancy (and slow) way to just do the last assignment.
Fortunately, the last assignment, F3=F2.lower()
lowercases the entire string F2
, which is exactly what you wanted to do, so it works out anyway. Which means you can replace all three of those lines with:
F3=F2.lower()
You also should always close files that you open. Since this can be tricky (e.g., in your function, you have to remember to close it in both the successful and error cases), the best way to do that is automatically, using a with
clause. Replace these two lines:
F = open(Filename)
F2=F.read()
with:
with open(Filename) as F:
F2=F.read()
After that, other than using a non-PEP-8 style, and performance problems if you have huge files, there's really nothing wrong with your code.