Reputation: 53
I'm trying to display text from a file but am getting the mentioned error. I've tried changing the default directory using os.chdir()
. I'm also getting the error even if I save the text file in the default directory. I'm using version 3.2.
Code:
from sys import argv
import os
script, filename = argv
txt = open(filename)
print(filename, "is the filename.")
print("Here is the text.", txt.read())
filename_again = input("Filename again pls.")
txt_again = open(filename_again)
print("Test again.", txt_again.read())
Error Report:
PS C:\Python32\Stuff\Programs> python Sample1.py fubar1.txt
Traceback (most recent call last):
File "Sample1.py", line 6, in <module>
txt = open(filename)
IOError: [Errno 2] No such file or directory: 'fubar1.txt'
PS C:\Python32\Stuff\Programs>
Upvotes: 0
Views: 2035
Reputation: 497
Tested the code and it worked for me. Tried to enter this as a comment, but don't have the points.
Tested on a Mac OS X Yosemite 10.10.2 using python 3.4.2
May I suggest it may have been a typo when entering the original filename? I tried different file names and it worked successfully. If this is not the case then debugging time.
Note Error Line The error is encountered in the first open statement so it is possible that the arguments text needs to be trimmed.
Debugging Idea
Noticed you are in windows. Try using a print()
command on line 5 to examine the contents of the filename variable. You may have spaces before or after the filename which are then causing the open statement to fail.
Upvotes: 1