Python Forum

Full Version: Getting FileNotFoundError: [Errno 2] ,if tries to acees same file from sub-directory
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I created a text file within the directory where I write my script file.
So when I tries to run the below code, it runs perfectly fine:

print(open("test.txt").read()) and 1
Now what I did is created a sub-directory and added another text file. I provided the absolute path to access and run the same above code with updated file path.
print(open("\Output\Hello.txt").read()) and 1
However, below error message is displayed:
Error:
FileNotFoundError: [Errno 2] No such file or directory: '\\Output\\Hello.txt'
I also try to add mode like read mode and still the same error displayed.

Did I made any mistake?
Regards,
Pinaki
use:
print(open(".\Output\Hello.txt").read()) and 1

what is intent of and 1 ?
Well
and 1
was to test how to get the contents of the files without any issue. In fact there are multiple way you can get the contents of the file without breaking anything. This is kind of security flaw.
For example:
[print(open("<filename.txt with absolute path>").read()),1][1]
print(open("<filename.txt with absolute path>").read()); or 1
you can try these statements. It's really fun to get the output :)