I am trying to use Pillow Library in Python. I am writing basic code to open up a picture that I have saved.
But every time I run the code, it says
"can't open file 'C:\Users\saadn\PycharmProjects\Giraffe\main.py':
[Errno 2] No such file or directory".
The path for where the picture is saved is "C:\Users\saadn\PycharmProjects\Giraffe\DirectoryAssign1"
Now I know that in the error it says "Girrafe\main.py". but I don't have a python file named main at all. Someone guide me.
the code I wrote was
from PIL import Image
img = Image.open("dany.jpg")
img.show()
Here is a screenshot for better understanding.
Actually I think there are 2 issues, and not 1.
Error 1 - Wrong File is being tried to run :
In the picture you attached there is an error which means it cannot open main.py (No such file) . You entered a wrong file name, your file name is OpenAndShow.py
How to fix?
Be sure to open the terminal or command prompt and be sure you are in the following directory C:\Users\saadn\PycharmProjects\Giraffe\ and enter the following command as you are a Windows OS User:
py OpenAndShow.py
Fun fact: Of course Python won't search in the whole PC or directory (including folders which are in the directory) , so that's why you need to mention it is in a folder in the Giraffe folder!
Error 2 - Wrong location for the picture
If you had fixed error 1, you will get another error, that dany.jpg is not such file, why ? Because it is in another folder inside the folder your code is located, you can use dany.jpg when they are in the same folder, not in another folder in the same folder, in this case, it is in DirectoryAssign1 which is in Girrafe folder.
How to fix?
To fix it, change the path:
From : The current Path:
dany.jpg
To : The following path below:
DirectoryAssign1\dany.jpg
You are running the wrong file. In PyCharm, afaik you can right click on the file and run. Your PyCharm is now trying to run the file main.py.
Otherwise, you can use the terminal:
python OpenAndShow.py
Related
I am trying to simply build a program, which reads another file. When I try to run the code I get the error mentioned in the topic. I've already tried to take the full path, but it wasn't working.
Do you have any ideas to solve the problem?
file = open("Text.txt")
vari = file.read()
print(vari)
When you're launching from a command line, the current working directory may not be the same as the home directory of your top-level file (i.e., the directory where your program file resides).
If you run it in cmd.exe (Command Prompt), then path to file "Text.txt" will be searched for in the directory currently opened in the Command line. Usually, C:\Users\[user]\ is the default working directory on Windows.
You need to run your program using Python interpreter/Py Laucher, that is usually opened on double click on *.py top-level program file or simply change the current directory in Command prompt with cd <TOP_LEVEL_FILE_DIR>.
You have to add the full path above file = open("Text.txt") line to indicate where this file is located. Adding the full path to the open(/path/to/where/this/text.txt) like an example is required in this case (so even if your main program is not in the same directory as the file you're trying to open, it will still work). There are many examples on SO, that show how this can be achieved.
Try the following code,
To open the file, use the built-in open() function.
fileLocation = open("C:/Users/Desktop/Text.txt", "r")
vari = fileLocation.read()
print(vari)
"r": read file "w": write file
It will read the file and will display the contents of the file.
Make sure you use the forward slash in the path.
I am running python file from command line. To be more specific my .py file is in location F:\new_github_code\cocktail_party\data\audio\audio_downloader.py>. I want to run audio_downloader.py file from the cocktail_party directory not from the audio directory.
I tried by using the following script from cmd. Any help will be appreciate
enter image description here
Try this. It will work:
python data\audio\audio_downloader.py
I encountered such a weird situation:
naivechou#naivechou/~>python test.py
test
naivechou#naivechou/~>pwd
/home/naivechou
naivechou#naivechou/~>python /home/naivechou/test.py
C:\toolchain\python\python.exe: can't open file '/home/naivechou/test.py': [Errno 2] No such file or directory
My working directory is /home/naivechou/, test.py is in there.
If I run test.py with absolute path, I'll get an error message of No such file or directory.But everything will be fine if I enter that directory and then run it. What's wrong with python?
Try moving into the folder where the python script is located and do a "ls" command there in Linux. if windows then do 'dir'. if you see the required file there then execute the following command
C:\location_where_the_script_is> python yourfile.py
For commands entered on the command line, Windows doesn't recognize forward-slashes as directory separators.
Your second example is looking in the current directory for the literal filename /home/naivechou/test.py, and of course such a filename does not exist.
Use backslashes instead, as is the Windows way:
python \home\naivechou\test.py
I am working through Learn Python the Hard Way but am having a problem with an exercise that reads a file: https://learnpythonthehardway.org/python3/ex15.html
I have followed this and made sure my file is in the same folder as my python file. I keep getting the error:
Traceback (most recent call last):
File "ex15.py", line 5, in <module>
txt = open(filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ex15_sample.txt'
I am entering: python ex15.py ex15_sample.txt into the powershell. I tried looking up the problem I'm having but could not find anything that helped in my case. Let me know if I am doing something wrong or if I'm missing something. Thanks
You can check your current path and whether your file exists by
import os
#get the current working directory
os.getcwd()
#check whether your file exists in the current working directory
os.path.exists("./ex15.py")
try 2 things below
Check both files .py and .txt are at same location. and then execute program.
this is how i ran the code.
C:\Users\db\Desktop\Python>python ex15.py
enter File Name: ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
If your file is at other location you need to provide absolute path for your file. as
C:\Users\db\Desktop\Python>python C:\Users\deepakb\Desktop\ex15_sample.txt
i had my txt file at desktop this time.
A brief demonstration on working directory and relative file locations..
Say we have a text file named hello.txt with the contents:
Hello World!
that is located at "C:\Users\username\Documents\python\hello.txt"
And we have a python script named script.py with the contents:
with open('hello.txt', 'r') as f:
print(f.read())
Which is located at "C:\Users\username\Documents\python\script.py"
If I just hit the windows key and type power shell and hit enter to open it, I'll see it by default gives me a prompt at my home directory: "C:\Users\username>". From here I can execute my python script by calling C:\Users\username> python C:\Users\username\Documents\python\script.py. If I do this, however, they python interpreter would have started in my home folder, and when it tries to find "hello.txt" it will only search through the folder "C:\Users\username". That's not where the file is, so we get a file not found error.
To resolve this we have a few options. Number one is to re-write our script file to include the full path of our text file: ... open("C:/Users/username/Documents/python/hello.txt", ... This way there is no searching necessary, and the script can be run from anywhere. Another option is to change the working directory of the interpreter from within the script. You can set the working directory with the os library by calling os.chdir('C:/Users/username/Documents/python'). Finally you can set the working directory with PowerShell before calling the python interpreter using the cd command:
C:\Users\username> cd .\Documents\python\
I had the same error.
It was a mistake from me naming the file as ex15_sample.txt.
When you are creating a file in notepad, the file name would be automatically .txt
so if you have created a file just rename it as ex15_sample.
or
You can also run the same program by giving python ex15.py ex15_sample.txt.txt
As you can see the file name might be ex15_sample.txt with a .txt extension.
I'm trying to open a file in python. Simple enough. The script that I am using is the same directory as my code, so I just use
myfile = open('file.txt', 'r')
This worked fine before, but now I am getting the error 'No such file or directory' (Errno2)
Why is this happening? I've used OS to check if I am in the right directory, and it is fine. What is python doing differently now than it did 20 minutes ago, when it found the file perfectly??
Assuming the file you are trying to open/read has appropriate permissions, the behavior is defined based on how you are invoking your python program. Let's assume your code and the file.txt are in ~/Desktop
If you are in ~/Desktop and do a python code.py your code will work fine. But if you are in say your home folder - ~ and do a python ~/Desktop/code.py then the python interpreter assumes your current working directory to be ~ and will return the error:
IOError: [Errno 2] No such file or directory: 'file.txt'
since it will not find file.txt in ~
Further, in the context of the given example:
os.getcwd()
returns the absolute path of your home directory and
os.path.realpath(__file__)
returns the absolute path of your python source file
Is it possible you are typing the name wrong, eg "test.fna" versus "test.fna.txt"?