Not finding any solution to [Errno 2] - python

I'm trying to use a program to read from a file with pi-digits. The program and the text file with the pi-digits are in the same directory, but i still get the error message :
with open('pi_digits.txt') as file_object:
contents = file_object.read()
print(contents.rstrip())
Traceback (most recent call last):
File "C:\Python\Python_Work\python_crash_course\files_and_exceptions\file_reader.py", line 1, in <module>
with open('pi_digits.txt') as file_object:
FileNotFoundError: [Errno 2] No such file or directory: 'pi_digits.txt'
I have looked for a solution but haven't found any.
I found a piece of code which supposedly shows me what the working directory is. I get an output that shows a directory that is 2 steps above the directory i have my programs and text file inside.
import os
cwd = os.getcwd() # Get the current working directory (cwd)
files = os.listdir(cwd) # Get all the files in that directory
print("Files in %r: %s" % (cwd, files))
So when i put the pi text document in the directory that the output is showing (>python_work), the program is working. When it does not work is when the text file is in ">files_and_exceptions" which is the same file the program itself is inside. My directory looks like this when it is not working:
>python_work
>python_crash_course
>files_and_exceptions
file_reader.py
pi_digits.txt
show_working_directory.py
And like this when it is working:
>python_work
pi_digits.txt
>python_crash_course
>files_and_exceptions
file_reader.py
show_working_directory.py
I'm new to python and really appreciate any help.
Thanks!

Relative path (one not starting with a leading /) is relative to some directory. In this case (and generally*), it's relative to the current working directory of the process.
In your case, given the information you've provided, for it would be "python_crash_course/files_and_exceptions/pi_digits.txt" in the first case as you appear to be running the script from python_work directory.
If you know the file to be in the same directory as the script itself, you could also say:
import os.path
...
os.path.join(os.path.dirname(__file__), "pi_digits.txt")
instead of "pi_digits.txt". Or the same using pathlib:
from pathlib import Path
...
Path(__file__).with_name("pi_digits.txt")
Actually unless you have a point to anchor to like the script itself, using relative filename / path (using absolute paths brings its own problems too) in the code directly is rather fragile and in that case getting it as a parameter of a function (and ultimately argument of CLI or script call in general) or alternatively reading it from standard input would be more robust.
* I would not make that an absolute statement, because there are situations and functions that can explicitly provide different anchor point (e.g. os.path.relpath or openat(2); or as another example a symlink target)

Related

FileNotFound Error: [Errno 2] No such file or directory b when iterating through list of files in Python on Windows?

I am getting a FileNotFound error when iterating through a list of files in Python on Windows.
The specific error I get looks like:
FileNotFoundError: File b'fileName.csv' does not exist
In my code, I first ask for input on where the file is located and generate a list using os (though I also tried glob):
directory = input('In what directory are your files located?')
fileList = [s for s in os.listdir(directory) if s.endswith('.csv')]
When I print the list, it does not contain byte b before any strings, as expected (but I still checked). My code seems to break at this step, which generates the error:
for file in fileList:
pd.read_csv(file) # breaks at this line
I have tried everything I could find on Stack Overflow to solve this problem. That includes:
Putting an r or b or rb right before the path string
Using both the relative and absolute file paths
Trying different variations of path separators (/, \, \\, etc.)
I've been dealing with Windows-related issues lately (since I normally work in Mac or Linux) so that was my first suspicion. I'd love another set of eyes to help me figure out where the snag is.
A2A.
Although the list was generated correctly because the full directory path was used, the working directory when the file was being run was .. You can verify this by running os.getcwd(). When later iterating through the list of file names, the program could not find those file names in the . directory, as it shouldn't. That list is just a list of file names; there was no directory tied to it so it used the current directory.
The easiest fix here is to change the directory the file is running through after the input. So,
directory = input('In what directory are your files located?')
os.chdir(directory) # Points os to given directory to avoid byte issue
If you need to access multiple directories, you could either do this right before you switch to each directory or save the full file path into your list instead.

changing directories in python - string not recognized

I am learning python and one of the operations I am trying to complete is to change directories. I am storing directory names in a dictionary and then trying to access these directories as follows :
Creating the directories :
for i in range(length):
try:
original_umask = os.umask(0)
os.makedirs(item_dict[i], 0755)
finally:
os.umask(original_umask)
for i in range (length):
os.chdir(item_dict[i])
This is working. However when I try to do further processing later by accessing these directories I run into the following error :
osdir = os.getcwd()
print " current working directory is " + osdir //works correctly
for i in range (length):
os.chdir((item_dict[i])
Traceback (most recent call last):
File ".\actions.py", line 40, in <module>
os.chdir((item_dict[i]))
WindowsError: [Error 2] The system cannot find the file specified: u'd-025c49f7-116e-4ad1-909e-13cc59b03dc3/'
Here 025c49f7-116e-4ad1-909e-13cc59b03dc3/ is the directory name . The same code works earlier as indicated. Any pointers ?
os.chdir() changes the current directory for the remainder of the Python script. You are probably better off not changing directories at all, and manipulating files in directories using relative paths.
In other words,
os.mkdir('a')
os.chdir('a')
os.mkdir('b')
os.chdir('b')
ends up creating a/b and leaving you inside this directory; attempting to os.chdir('b') again fails, because there is no b inside this directory - you only just created it, so it is obviously empty.
I'm guessing you wanted to create a and b as subdirectories of the current directory, not the latter inside the former. Not doing os.chdir('a') after creating a obviously fixes this; and more generally, unless you specifically want to recursively create a deep sequence of nested directories, there is rarely a need for your programs to switch their working directory.
Needless to say,
os.chdir('a')
newfile = open('c', 'w')
can be rephrased without changing the working directory;
newfile = open(os.path.join(['a', 'c']), 'w')
where in this trivial case open('a/c', 'w') works as well; but if you do nontrivial processing where directory names and/or file names are in variables, you need to know about the more general os.path.join() syntax.

Error in opening a file in python

Let's consider the below code:
fp=open('PR1.txt','r')
ch=fp.readlines()
print "%s" % (' '.join(ch))
print "\n"
fp.close()
Above code gives an error:
IOError: [Errno 2] No such file or directory: 'PR1.txt'
But when i am provididng its full location i.e;
fp=open('D:/PR1.txt','r')
then it is working properly...
IS it necessary to provide full location of file or there is some other way too?
No, it is not necessary, but you need to be certain you are running your script with the right working directory. Your script working directory is evidently not D:/.
In practice, it is better to only use relative paths if you are in full control of the working directory. You can get the current working directory with os.getcwd() and set it with os.chdir() but using absolute paths is usually better.
For paths relative to the current module or script, use the __file__ global to produce a directory name:
import os.path
here = os.path.dirname(os.path.absolute(__file__))
then use os.path.join() to make relative paths absolute in reference to here.

Absolute Path and Relative Path issue in python

How to remove path related problems in python?
For e.g. I have a module test.py inside a directory TEST
**test.py**
import os
file_path = os.getcwd() + '/../abc.txt'
f = open(file_path)
lines = f.readlines()
f.close
print lines
Now, when I execute the above program outside TEST directory, it gives me error:-
Traceback (most recent call last):
File "TEST/test.py", line 4, in ?
f = open(file_path)
IOError: [Errno 2] No such file or directory: 'abc.txt'
how to resolve this kind of problem. Basically this is just a small example that I have given up.
I am dealing with a huge problem of this kind.
I am using existing packages, which needs to be run only from that directory where it exists, how to resolve such kind of problems, so that I can run the program from anywhere I want.
Or able to deal with the above example either running inside TEST directory or outside TEST directory.
Any help.?
I think the easiest thing is to change the current working directory to the one of the script file:
import os
os.chdir(os.path.dirname(__file__))
This may cause problems, however, if the script is also working with files in the original working directory.
Your code is looking at the current working directory, and uses that as a basis for finding the files it needs. This is almost never a good idea, as you are now finding out.
The solution mentioned in the answer by Emil Vikström is a quickfix solution, but a more correct solution would be to not use current working directory as a startingpoint.
As mentioned in the other answer, __file__ isn't available in the interpreter, but it's an excellent solution for your code.
Rewrite your second line to something like this:
file_path = os.path.join(os.path.dirname(__file__), "..", "abc.txt")
This will take the directory the current file is in, join it first with .. and then with abc.txt, to create the path you want.
You should fix similar usage of os.getcwd() elsewhere in your code in the same way.

file error in python

I have the following script:
import os
import stat
curDir = os.getcwd()+'/test'
for (paths, dirs, files) in os.walk(curDir):
for f in files:
if os.stat(f)[stat.ST_SIZE]>0:
print f
and the folder test/:
test_folder:
--test.wav
a.exe
t1
t2
rain.wav
when i run this script with geany it gives the following error:
Traceback (most recent call last):
File "new_folder_deleter.py", line 8, in <module>
if os.stat(f)[stat.ST_SIZE]>0:
OSError: [Errno2] No such file or directory: 'a.exe'
but when I run it with IDLE:
it just prints test.wav in subfolder test_folder
Can anyone explain why it is so and how I can fix it?
P.S:
My aim is to browse all files and delete files with specified sizes.
You need to specify a full path for os.stat, unless the file is in the current working directory. The simplest way to fix this is to change the WD before trying to access the files:
curDir = os.getcwd()+'/test'
os.chdir(curDir)
A more general solution is to pass the full path to os.stat:
if os.stat(os.path.join(paths, f))[stat.ST_SIZE]>0:
print f
I am not quite sure why IDLE does not produce an error here, though.
The filename is only the basename. You need to use os.path.join(path, f).
The list of files that's returned in the files component from os.walk() is just the file names, without the path. Before you can perform any operations on those files (including stat()), you need to reassemble the path to the file.
The os.walk function returns file and directory names relative to current folder, so you need to os.stat(os.path.join(paths, f)).

Categories