Import a single file from another directory - python

I'm kind of new with python and there is something troubling me. I'm using pandas to read an excel file. All works well if I have the excel in the same directory as my .py file.
What I want to know is what is the best way to get a file that is in a completely different path. I've searched a lot and haven't found a straightforward answer. I've seen examples with sys.append, examples with external libraries, etc. I am trying to understand the pros/cons as well of each solution. Ideally I would like to have the file path as a user input

sys.path is where Python tries to locate modules or packages to import them. Not files when you are trying to access them. (like your excel file)
On the other hand, open built-in function or Pandas read functions require a path to a file. As long as you work with absolute paths you don't need to worry about it.
If you want to give it a relative path, first check to see where you are and then locate your file relative to your working directory.
with os.getcwd() you can get your current working directory.
As an example, suppose we have:
tmp/
└── inside_directory
├── pfile.py
└── text.txt
If my terminal indicates that I'm in tmp directory and I run my pfile.py with:
python inside_directory/pfile.py
and the content of pfile.py be:
with open("text.txt") as f:
print(f.read())
I will get error which says: FileNotFoundError: [Errno 2] No such file or directory: 'text.txt'.
Why? because I used relative path and there is no text.txt in the tmp directory.
Then what are the solutions?
Option-1: use open("inside_directory/text.txt") instead of open("text.txt").
Option-2: first go to inside_directory then use open("text.txt") and run your script with python pfile.py.
Option-3: Use absolute path. You can access that file regardless of where you are now.

Related

EZ Sheets Python credentials-sheets.json

Does the credentials-sheets.json file and two tokens for drive and sheets need to be in the same folder as my python script, or can they reside somewhere else on my machine.
For example, I've got a test.py file on my desktop and ezsheets works correctly if the json file and two tokens are also on the desktop.
Do I need to copy/paste these files into the same folder as my python project, or is there somewhere else I can store them where ezsheets will find them? It seems kind of redundant to have to copy paste them every time, but I'm currently in my first coding class so perhaps I don't know best practices.
It's ideal if you can have the JSON file in the same folder, but not required. I'm unfamiliar with ezsheets, but I think it will work anywhere if you specify the correct file type. You should put the JSON file and the two tokens in the same folder (but not required).
For adding the file in the same folder, you can make use with the sys module.
import sys
path = sys.argv[0]
The path variable will have the script path. You can use this knowledge to determine the other files' path.

Is there a way to load images without using the root file in pygame?

When I load images using pygame, I normally just use file locations such as: 'B:\Python\Images\image.png' but if I want this to be able to run on different computers without needing to edit the code every time, I would just want to go like to do 'image.png' instead. Is there a way to do this? Whenever I try to skip the B:\Python\' part, I get the error: "File Error: No file "image.png" found in working directory 'C:\WINDOWS\System32."
I've seen people use a file location such as: '../Images/image.png' instead but that didn't work (I may have done it wrong).
You have to set the working directory. The resource (image, font, sound, etc.) file path has to be relative to the current working directory. The working directory is possibly different to the directory of the python script. The name and path of the python file can be retrieved with __file__. The current working directory can be set with os.chdir(path).
Put the following at the beginning of your main script to set the working directory to the same as the script's directory. This allows you to specify file paths relative to your main file:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))

FileNotFoundError: [Errno 2] No such file or directory: 'test_text.txt'

The file "test_text.txt" is in the same folder as the program. it's a seperate folder from everything else that just has the text file and the program. unless I am misunderstanding what the error means, I just can't figure out what i'm missing or did wrong. i'm just reading a basics python book and this I pretty much copied it 1 for 1.
CODE
with open('test_text.txt') as test_text:
reader = test_text.read()
print (reader)
FileNotFoundError means you are trying to open a file that does not exist in the specified directory (in this case, whatever directory you are running your Python script from).
the file "test_text.txt" is in the same folder as the program. it's a seperate folder from everything else that just has the text file and the program
In that case, you need to make sure that you're in the same directory as your file on your command-line, or specify the full path to test_text.txt (e.g. /home/user/Desktop/test_text.txt)
FileNotFoundError occurs when you are trying to access files outside the scope of the application. In this case, the scope of your application is the folder in which you main python file resides.
There are couple of ways through which you can resolve this:
1. Provide an absolute or full path to the file: As mentioned by #pigeonburger in his answer, you can provide a full path. If you are using windows, then full path will be from C:/Desktop/
2. Using a Relative Path: Relative paths is the solution if both the file you want to access and the python program are in the same folder. Relative Paths start with ./ So, based on your file structure, the possible solution is this:
with open('./test_text.txt'):
# your code
Thank you Pointman for your answer. It did not work for me but it did lead me to what worked. I am using Visual Studio Code and I was having this issue with opening a text from a Udemy course.
jabber = open('Jabberwocky.txt', 'r')
for line in jabber:
print(line)
jabber.close()
After running this code I would get the following error message
FileNotFoundError: [Errno 2] No such file or directory: './Jabberwocky.txt'
I tried both the absolute path and the relative path option but they did not work for me. What I found that worked was once the terminal is open using Python you will have to traverse through your files until you get to the said folder or file. Once you do this then run the code and it will work.

How to find path to current .py file in Spyder (Anaconda)?

Set-up
I run a script on my computer, located in the directory Users/path/to/my/script.py.
In the script, I use the path to the script, e.g.,
sub_path = 'Users/path/to/my/'
os.chdir(sub_path + 'other_script/')
As you can see, I define sub_path in the code 'manually'.
Problem
I don't want to define the sub_path manually, I'd rather have Python do it for me.
I'm looking for something similar to the code I use to obtain the current working directory: os.getcwd(), but then a code to obtain the directory of the current file.
I mainly find answers similar to this one, which says,
os.path.abspath(os.path.dirname(__file__))
but in the Spyder & Anaconda set-up, this generates a NameError: name '__file__' is not defined.
What can I do?
You if you want to move back one folder/directory you use the .. in your file path.
os.chdir('../other_scripts/')
will work. You may fine it helpful to view this or the wiki.
If you want to move from where you currently are you can use './new_dir/'. If you want to automate how to find other files you may want to read here which says to use os.walk. This may be the same question.
Mark8888 pointed out to run the whole script (run file (F5)) instead of just pieces of the script
this way multiple approaches should work to get the script file location and change the current working directory
import os
# directory of script file
print(os.path.abspath(os.path.dirname(__file__)))
# change current working directory
os.chdir(os.path.abspath(os.path.dirname(__file__)))
# current working directory
print(os.getcwd())
also
import os
import sys
# directory of script file
print(os.path.abspath(os.path.dirname(sys.argv[0])))
# change current working directory
os.chdir(os.path.abspath(os.path.dirname(sys.argv[0])))
# current working directory
print(os.getcwd())
I add the following lines to any script I run in case I need to access data relative to the location of the script
import sys
script = sys.argv[0]
print(script)
'C:/SomeFolder/A_Subfolder/CurrentlyRunningScript.py' # changed obviously
First, save your Jupyter Notebook. Second, locate the directory your Jupyter Notebook is stored in. Thirdly, ensure that your Jupyter Notebook and CSV file are in the same place.

Getting current script path: __path__[0] or os.path.dirname(os.path.abspath(__file__))

What is the best practice to get the path of the currently executed script?
path = os.path.dirname(os.path.abspath(__file__))
or
path = __path__[0]
When tried, they both output the same result.
But is one more rigorous than the other?
Is one more cross platform?
The doc says:
This variable can be modified; doing so affects future searches for
modules and subpackages contained in the package. While this feature
is not often needed, it can be used to extend the set of modules found
in a package.
Apart from me modifying it explicitly, can it be modified by the interpreter without me knowing?
If so, would that return a wrong path, or is the first element always pointing to the current directory of the script?
I need that to access a config file which would be stored inside a folder called config/ in my package.
But open("config/config.txt", 'rt') does not work, it returns a IOError: [Errno 2] No such file or directory
Thanks.

Categories

Resources