open() gives FileNotFoundError/IOError: Errno 2 No such file or directory
- Make sure the file exists: use
os.listdir()
to see the list of files in the current working directory - Make sure you're in the directory you think you're in with
os.getcwd()
(if you launch your code from an IDE, you may well be in a different directory) - You can then either:
- Call
os.chdir(dir)
,dir
being the folder where the file is
located, then open the file with just its name like you were doing. - Specify an absolute path to the file in your
open
call.
- Call
- Remember to use a raw string if your path uses backslashes, like
so:dir = r'C:\Python32'
- If you don't use raw-string, you have to escape every backslash:
'C:\\User\\Bob\\...'
- Forward-slashes also work on Windows
'C:/Python32'
and do not need to be escaped.
- If you don't use raw-string, you have to escape every backslash:
Let me clarify how Python finds files:
- An absolute path is a path that starts with your computer's root directory, for example
C:\Python\scripts
if you're on Windows. - A relative path is a path that does not start with your computer's root directory, and is instead relative to something called the working directory. You can view Python's current working directory by calling
os.getcwd()
.
If you try to do open('sortedLists.yaml')
, Python will see that you are passing it a relative path, so it will search for the file inside the current working directory.
Calling os.chdir()
will change the current working directory.
Example: Let's say file.txt
is found in C:\Folder
.
To open it, you can do:
os.chdir(r'C:\Folder')
open('file.txt') # relative path, looks inside the current working directory
or
open(r'C:\Folder\file.txt') # absolute path
PyInstaller - FileNotFoundError: [Errno 2] No such file or directory
I found an answer to my own question. I still don't know why it couldn't find the palettable library but I was able to get it working by putting this in the command window:
pyinstaller --collect-data palettable --onefile main.py
This allowed it to I guess collect it manually though I'm still not sure how it wasn't able to find palettable in the first place since its installed the same way as all the other dependent libraries. Hopefully this helps anyone with a similar issue.
Related Topics
Extracting Text from a PDF File Using PDFminer in Python
Placing Custom Images in a Plot Window--As Custom Data Markers or to Annotate Those Markers
Negative Integer Division Surprising Result
Parallel Processing from a Command Queue on Linux (Bash, Python, Ruby... Whatever)
Python Ctypes - Loading Dll Throws Oserror: [Winerror 193] %1 Is Not a Valid Win32 Application
Numpy Selecting Specific Column Index Per Row by Using a List of Indexes
Parsing Boolean Values with Argparse
Priority of the Logical Operators Not, And, or in Python
How to Get a List of All the Duplicate Items Using Pandas in Python
Sharing a Result Queue Among Several Processes
Why Isn't My Pandas 'Apply' Function Referencing Multiple Columns Working
Why Is a List Comprehension So Much Faster Than Appending to a List
Case Insensitive Regular Expression Without Re.Compile
How to Prevent Numbers Being Changed to Exponential Form in Python Matplotlib Figure
Why Python Recursive Function Returns None