New path is not reflected in Python code - python

I have a code in which I am loading a text file from a folder. The code looks like this:
snap1 = np.loadtxt("../data/milli_17")
snap2 = np.loadtxt("../data/milli_19")
So the milli17 and milli19 files are located in a folder that is located in the same folder with my working folder. So far everything was good. However, I moved the data folder inside the working directory so the directory placement became like this: /Workingdirectory/data/
So I went ahead and reflected that on the code by removing the two dots so it wouldn't go up one directory:
snap1 = np.loadtxt("/data/milli_17")
snap2 = np.loadtxt("/data/milli_19")
However now when I run the code I get an error saying the directory does not exist:
IOError: [Errno 2] No such file or directory: '../data/milli_17'
but debugging shows the line of error as this:
----> 4 snap1 = np.loadtxt("/data/milli_17")
Couldn't get my head around it, all seemed OK to me. Where do I make the mistake?
Edit:
I don't think the problem has something to do with how I write down the path. The problem is that it doesn't matter what I put there, the code still (as seen in the error code) goes and checks the old directory.

If you restart the kernel that should resolve your issue.

I believe you have to use the file extension and no forward slash before the data folder.
snap1 = np.loadtxt("data/milli_17.txt")

Related

shutil.copyfile() throws Errno 2 exception on destination file path

To setup this question, I'm using Python in the form of Jython 2.7 (in Ignition SCADA). I have a function that copies image files to a print spool network folder. Occasionally I get an error like the following:
An ERROR occured: Line - 241: <type 'exceptions.IOError'>, [Errno 2] No such file or directory: u'\\server23\eFormz\D1234567.tif'. Part - ABC1234X12, while printing label.
These print jobs run through a number of different parts in the same order but only 1 or 2 generate this error. All others copy the file as expected. The error is calling out the DESTINATION file path in all cases. The destination folder exists as all of the good copies are going to the same folder as those that cause errors.
The code that does the copy looks like this:
import shutil
imgFiles = glob.glob(labelImage)
if len(imgFiles) > 0:
imgFile = imgFiles[0]
#ensure the image file path is actually a file
if os.path.isfile(imgFile):
fileName = imgFile.rsplit('\\', 1)[1]
dstFP = '\\\\server23\\eFormz\\' + fileName
shutil.copyfile(imgFile, dstFP)
Since this works most of the time, with the same folder paths and only the file name changing (all the source files are confirmed to exist and confirmed that they are indeed files, before attempting the copy), I'm perplexed as to the cause. File and folder permissions are the same for each so that should not be the cause.
Any ideas as to what may be causing this or a way to more effectively troubleshoot it would be appreciated.
Thanks.

Python: "../DATA/file.csv" no longer working for file opening

Since a while, I have been using the short syntax ('../DAT/file.csv') to get to files under DATA folder. Since this morning, it is not working anymore and I am getting the following error:
FileNotFoundError: [Errno 2] No such file or directory: '../DATA/file.csv'
Any thoughts? Code I am using is below:
Thanks in advance,
df = pd.read_csv('../DATA/moviereviews.csv')
Check if your current directory is the one you expect it to be (i.e. the one that is beside the DATA directory). You can use the following to do so:
import os
print(os.getcwd())
Check that DATA/moviereviews.csv actually exists.

How do I have to solve Pygame couldn't open image error?

background = pygame.image.load('example.jpg')
nubjuk = pygame.image.load('nubjuk.png')
nubjuk = pygame.transform.scale(nubjuk, (200,90))
This is the code part of the problem and the error message is:
Exception has occurred: error
Couldn't open example.jpg
I think I've tried every solution uploaded in other StackOverflow questions.(os.path ~, absolute path, just anything) However, they don't work.
More weird thing is that it properly worked yesterday, but even though I didn't touch anything it doesn't work today.
What's the problem?
Your code expects the files 'example.jpg' and 'nubjuk.png' to be in the current working directory.
Obviously this is not the case. Either you changed the working directory prior to calling your script or your script changes the working directory.
or you moved the png files to a different directory (or deleted them)
If the .png files are in the same directory than your python script you can do something like that
at the beginning of your file.
import os
MYDIR = os.path.realpath(os.path.dirname(__file__))
and then replace your two image.load lines with
background = pygame.image.load(os.path.join(MYDIR, 'example.jpg'))
nubjuk = pygame.image.load(os.path.join(MYDIR, 'nubjuk.png'))

filenotfound error python (running through atom)

I'm working my way through the python crash course pdf. Everything was going well until I hit chapter 10 "files and exceptions".
The task is very simple.
1) create a text file "pi_digits.txt" that contains the first 30 digits of pi.
2) run the following code:
with open('pi_digits.txt') as file_object:
contents = file_object.read()
print(contents)
I keep getting a filenotfounderror [errno 2].
I have tried using the full file path, placing the file in the same ~.atom folder that contains the package 'script'.
I tried to run the file through a terminal and got the same error message.
I also searched stackoverflow for solutions and did find similar problems but the answers did not work.
Any help would be appreciated.
Prepend this:
import os
print(os.getcwd())
os.chdir('/tmp')
and copy the .txt file to /tmp. Also, be sure the copied filename is all lowercase, to match your program.

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.

Categories