FileNotFoundError: [Errno 2] No such file or directory in Python
I'm running my code in Python Ver3.9(Window10) and get the error status: FileNotFoundError: [Errno 2] No such file or directory, here my code:
path = "Image/text.txt"
file = open(path,'r').read()
print(file)
In my program, I have a lot of files to call, I need a solution to solve it thoroughly on my computer and others too, with different paths but all calling a file name. Help me, many tks guys!
- M1
Mary Christ Dec 30 2020
Okey, here my solutions :
Step 1: check your file name and path again.
Step 2: Find a correct full path with your filename like this form:
FOLDER(If any)/FILENAME - If you run the code in the normal way: full_path = "D:/Users/Admin/Desktop/FOLDER/FILENAME.txt" E.g: full_path = "D:/Users/Admin/Desktop/Image/text.txt"
Step 3: Put the path form in your code:
path = "D:/Users/Admin/Desktop/Image/text.txt" file = open(path,'r').read()#print(file)
NOTE: IF YOU DON'T LIKE TO USE a correct full path with your filename like this form, you can use like that(not availability if you run your code in COMMAND PROMPT(CMD):
#FOLDER = "Image" #FILENAME = "text.txt" path = "Image/text.txt"
Here my code manually find exactly 1 path when there is only file name:
#!/usr/bin/python import time, os #FOLDER = "Image" #FILENAME = "text.txt" yourpath = "Image/text.txt" def Find_Path(yourpath): find_path = os.path.abspath(yourpath) link_path = find_path.replace("\\", "/").split('!') print("FULL LINK PATH: ",link_path) for filename in link_path: print("YOUR FILE NAME: ",filename) read_file = open(filename,'r').read() print(read_file) #time.sleep(30) Find_Path(yourpath)
I hope my solution useful for you, If you still have questions, please comment below for my support:)))
* Type maximum 2000 characters.
* All comments have to wait approved before display.
* Please polite comment and respect questions and answers of others.