Python Forum
Getting FileNotFoundError: [Errno 2] ,if tries to acees same file from sub-directory
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting FileNotFoundError: [Errno 2] ,if tries to acees same file from sub-directory
#1
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
Reply
#2
use:
print(open(".\Output\Hello.txt").read()) and 1

what is intent of and 1 ?
Reply
#3
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 :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error (Errno 2), File upload with the Flask framework and a public IP Username_Python1 0 130 Yesterday, 01:46 PM
Last Post: Username_Python1
  FileNotFoundError: [WinError 2] The system cannot find the file specified NewBiee 2 1,496 Jul-31-2023, 11:42 AM
Last Post: deanhystad
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,080 Jun-27-2023, 01:17 PM
Last Post: diver999
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 928 Jan-23-2023, 04:56 AM
Last Post: deanhystad
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,065 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  no such file or directory in SFTP saisankalpj 2 1,493 Nov-25-2022, 11:07 AM
Last Post: DeaD_EyE
Photo Making Zip file of a file and Directory Nasir 2 985 Oct-07-2022, 02:01 PM
Last Post: Nasir
  Failed to execute child process (No such file or directory) uriel 1 1,616 Sep-15-2022, 03:48 PM
Last Post: Gribouillis
  Need Help: FileNotFoundError:[Errno 2] No such file or directory python202209 5 2,524 Sep-12-2022, 04:50 AM
Last Post: python202209
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,330 May-17-2022, 07:49 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020