Why does Numpy.loadtxt work when called from IDLE and gives an IOError when called from shell?
Why does Numpy.loadtxt work when called from IDLE and gives an IOError when called from shell?
I try to do the following:
1 VBA script calls shell using
RetVal = Shell(fullpythonexepath & fullscriptpath)
2 Shell get follwing command
fullpythonexepath & fullscriptpath
3 Python Script
import numpy as np
thefilepath = "input.txt" # is in the same folder as the script
theoutputpath = "output.txt" # is in the same folder as the script
ssc=np.loadtxt(thefilepath, delimiter='t', skiprows=0, usecols=range(2)) #error line
#some other code to fit input data
np.savetxt(theoutputpath, sscnew, header=top, fmt='%1.2ft%1.2f')
When I run this the output file doesn't get printed, which means the script doesn't run properly.
Now to the funny thing: When I run the python script from IDLE it runs fine. When I run it from the shell using the command:
fullpythonexepath & fullscriptpath
it says :
I tried inputting the fullpath to the input.txt and output.txt. When I do this and run it in the IDLE it does not find the file anymore. When called from the shell it doesn't work either.
Python obviousely does not find the path to the input.txt
the issue is, to my understanding, not related to VBA. The error occures also when using shell commands
Solution was to put :
import os
os.chdir(r"fullpath")
into my script this changes the current working directory to my path and then input.txt gets found.
1 Answer
1
When you start the script from shell the working directory for the script will be the directory from which it is called upon.
Most IDLE define their own working directory.
To check I suggest doing:
os.getcwd()
in both cases and look what directory is used in both cases
both shell and IDLE print the same path.. But I think it has to be something like that. Why else should the shell not be able to find the input.txt file..
– Lucas Raphael Pianegonda
Aug 20 at 11:48
no wonder, the sys.argv[0] is always the path the script, see the edit for what you need
– Sharku
Aug 20 at 11:51
worked, thanks mate!
– Lucas Raphael Pianegonda
Aug 20 at 12:08
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
"When I run this the output file doesn't get printed, which means the script doesn't run properly." Huh? You don't print anything in that script
– juanpa.arrivillaga
Aug 20 at 11:58