0

This python code right here should open a text file in the same directory where I put my ex15.py and read it but there seems to be a problem it always says there's a problem in line 5 and it can't the text file in the same folder:

C:\Users\GAMING>py C:\MyCodes\PythonCodes\ex15.py ex15_sample.txt
Traceback (most recent call last):
  File "C:\MyCodes\PythonCodes\ex15.py", line 5, in <module>
    txt = open(filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ex15_sample.txt'

Code:

from sys import argv

(script, filename) = argv

txt = open(filename)

print(f"Here's your file {filename}:")
print(txt.read())

print("Type the filename again:")
file_again = input("> ")

txt_again = open(file_again)

print(txt_again.read())                            
Jon
  • 17
  • 5
  • @yudhiesh If the file is in the same current working directory, there is not need for an absolute path. – Jacob Lee Jan 30 '21 at 05:43
  • 1
    The problem is that you are running from `C:\Users\GAMING` which is the cwd in your program. The file would open if it was present in the cwd. – Abdul Aziz Barkat Jan 30 '21 at 05:43

0 Answers0