python can not find the fits file when using sys.path.insert - python

I would like to access the output of another python program that extracts information from a fits file.
I usually do this in the following way:
import sys
sys.path.insert(1, '/software/xray/Python_scripts')
from program2 import results
However, in this case, I receive the following error:
FileNotFoundError: [Errno 2] No such file or directory: 'info.fits'
When I run the program2.py It runs without problem. So, I don't understand why when I call it from program1.py it does not recognize the fits file, therefore it doesn't give the results! Can anybody point me in the right direction?
Thanks.

Seems like your import from program2 import results searching for a file named info.fits which is located most probably in '/software/xray/Python_scripts'.
Basically, sys.path.insert temporarily adds path to PATH, in order to make OS executing/importing script from that place. It doesn't mean that it becomes a file search path as well.
You need to do somethig like:
import os
cwd = os.getcwd()
os.chdir('/software/xray/Python_scripts')
from program2 import results
os.chdir(cwd)
It is certainly a crunch, I would suggest you to create a package out of your program2 module. https://realpython.com/python-modules-packages/

Related

WinError 2, python cant find my file when os.remove

printing the name of the files goes fine but when I try to use os.remove it says it cannot find the file. I also tried full path but also didnt work.
here is my code
import os
for tempfiles in os.listdir(r"All Posts"):
os.remove(tempfiles)

Python import module load_source

I have question on the load_source.
when my 2 .py files are in the same directory /home/pi they work fine.
main.py
#!/usr/bin/python
import buttonlog
buttonlog.py
import datetime
i = datetime.datetime.now()
#OPEN FILE & APPEND
f=open('buttonlog.txt','a')
#WRITE DATE THEN NEW LINE WITH THE '\N'
f.write(i.isoformat() + '\n')
When I run python main.py it writes an entry like I'd expect.
However I'd like to store main.py in another directory so I tried this, it is stored in the /home/pi/test
#!/usr/bin/python
import imp
imp.load_source('buttonlog', '/home/pi/buttonlog.py')
When I run python /home/pi/test/main.py I do not get any errors nor does it write an entry into my file. What am I doing wrong?
The secret is the use of the open command.
As the documentation says about the first argument,
file is a path-like object giving the pathname (absolute or relative to the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped.
By passing just "buttonlog.txt", this is not an absolute pathname, so it's relative to the current working directory.
The simplest way to fix this is to use a full path. If you always want it writing in to /home/pi, you just need:
f=open('/home/pi/buttonlog.txt','a')
There are other alternatives, though I think this is the cleanest. You could also change your current working directory prior to issuing the open command for the same results. Simply put this code above the open line:
import os
os.chdir("/home/pi")

Opening a CSV from a Different Directory Python

I've been working on a project where I need to import csv files, previously the csv files have been in the same working directory. Now the project is getting bigger so for security and organizational resaons I'd much prefer to keep them in a different directory.
I've had a look at some other questions asking similar things but I couldn't figure out out how to apply them to my code as each time I tried I kept getting the same error message mainly:
IOError: [Errno 2] No such file or directory:
My original attempts all looked something like this:
import csv # Import the csv module
import MySQLdb # Import MySQLdb module
def connect():
login = csv.reader(file('/~/Projects/bmm_private/login_test.txt'))
I changed the path within several times as well by dropping the first / then then the ~ then the / again after that, but each time I got the error message. I then tried another method suggested by several people by importing the os:
import os
import csv # Import the csv module
import MySQLdb # Import MySQLdb module
def connect():
path = r'F:\Projects\bmm_private\login_test.txt'
f = os.path.normpath(path)
login = csv.reader(file(f))
But I got the error message yet again.
Any help here would be much appreciated, if I could request that you use the real path (~/Projects/bmm_private/login_test.txt) in any answers you know of so it's very clear to me what I'm missing out here.
I'm pretty new to python so I may struggle to understand without extra clarity/explanation. Thanks in advance!
The tilde tells me that this is the home folder (e.g. C:\Users\<username> on my Windows system, or /Users/<username> on my Mac). If you want to expand the user, use the os.path.expanduser call:
full_path = os.path.expanduser('~/Projects/bmm_private/login_test.txt')
# Now you can open it
Another approach is to seek for the file in relative to your current script. For example, if your script is in ~/Projects/my_scripts, then:
script_dir = os.path.dirname(__file__) # Script directory
full_path = os.path.join(script_dir, '../bmm_private/login_test.txt')
# Now use full_path

How to set current working directory in python in a automatic way

How can I set the current path of my python file "myproject.py" to the file itself?
I do not want something like this:
path = "the path of myproject.py"
In mathematica I can set:
SetDirectory[NotebookDirectory[]]
The advantage with the code in Mathematica is that if I change the path of my Mathematica file, for example if I give it to someone else or I put it in another folder, I do not need to do anything extra. Each time Mathematica automatically set the directory to the current folder.
I want something similar to this in Python.
The right solution is not to change the current working directory, but to get the full path to the directory containing your script or module then use os.path.join to build your files path:
import os
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
# then:
myfile_path = os.path.join(ROOT_PATH, "myfile.txt")
This is safer than messing with current working directory (hint : what would happen if another module changes the current working directory after you did but before you access your files ?)
I want to set the directory in which the python file is, as working directory
There are two step:
Find out path to the python file
Set its parent directory as the working directory
The 2nd is simple:
import os
os.chdir(module_dir) # set working directory
The 1st might be complex if you want to support a general case (python file that is run as a script directly, python file that is imported in another module, python file that is symlinked, etc). Here's one possible solution:
import inspect
import os
module_path = inspect.getfile(inspect.currentframe())
module_dir = os.path.realpath(os.path.dirname(module_path))
Use the os.getcwd() function from the built in os module also there's os.getcwdu() which returns a unicode object of the current working directory
Example usage:
import os
path = os.getcwd()
print path
#C:\Users\KDawG\Desktop\Python

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