Problems executing python script in notepad++ - python

I have some images that load in my python script.
they reside in c:\Python27\subfolder\images\
in my .py file (which resides in c:\Python27\subfolder\ )
I load them with ./images/file.jpg
It works just fine in IDLE
However when I run the file through notepad++ I get the error:
Failed to load file ./images/file.jpg
How can I fix this with out having to change my actual python code? (It works if I load the images with the full path, but I dont want to do this).
Im using the run command C:\Python27\python.exe "$(FULL_CURRENT_PATH) in notepad++
thank you very much!!

Well to help you fix the problem, you should do this
import os
print os.getcwd() #gets the current working directory
Most likely your problem is that within the IDE, your CWD differs than when you're running it from the console.
If you want it to work like it does in the IDE, you should first get yourself (the console) within that directory, via navigation (os.chdir(...) command).
You could also make a batch/bash file that runs the python script from the directory you need, and call that file from wherever you want (it would still call the python script with the path you gave

Related

Code not able to locate file from within the same path as the program itself

My program is located under C:\Users\Username\pycode.py
My file to be called within the program is located under C:\Users\Username\<filename>
The file and the code are in the same directory.
When I call from within the program using the below set of codes,
import os; import sys
with open(os.path.join(sys.path[0], "file1.txt"), "r") as fn1:
print(fn1.read())
the Py code picks up the file and executes the applied methods on it.
However, when I don't use the above code and use as:
fn1 = open("file1.txt", "r")
print(fn1.read())
There is only an error that the file does not exist.
Any thoughts and ideas, what is missing to make the file within the local directory as the py code to recognize the file.
If it's relevant, I'm a beginner using VSCode on Windows 10.
Speaking from personal experience, it happens to me when I am using vscode and I am in a directory say d:/Desktop/python programs. But the file I want to open() is in the directory d:/Desktop/python programs/fun.
In this case it is not sufficient for me to use the path like "filename". Instead I need to use "/fun/filename". This is even though the file I am working on is in the fun folder.
So perhaps you could go to the terminal in VSCode (your IDE) and run cd "path to your file" and that should solve the problem
Try running the following code and see what output you get
import os
print(os.getcwd())
In my case this would print d:/Desktop/python programs

AREPL and console showing different working directories (Visual Studio Code User) - why?

(I'm currently learning Python so I might be missing something obvious - sorry if this is the case)
I'm using Visual Studio Code and the AREPL extension (to get an idea of what will show up while I'm writing code) to learn Python coding
However, I encountered a weird error, where different working directories seem to be registered for AREPL and the console (sorry I'm new so my descriptions and wording might be inaccurate)
Specifically, if I type
cwd = os.getcwd()
print(cwd)
on the AREPL panel, it shows the folder containing the python file I'm working on as the working directory (C:\Users\XXXXXX\Coding\Python),
but when I actually run the code, the console shows the upper folder (directory?) (C:\Users\XXXXXX\Coding )
So when when I try to open different files in the C:\Users\XXXXXX\Coding\Python folder using a relative path (e.g. open("temp.txt", "r")) then it runs without error on the AREPL window and displays the expected results, but shows an error when I actually run it through console
This is also weird because I remember having used relative paths in the past without problem when I was working on a file in a different folder
FYI, I never changed any settings related to working directory or manually set them until now
I know that the obvious easy answer is to use an absolute path, but I'm just trying to figure out what might be causing the error to better learn about Python (or Visual Studio Code)
Thank you!
The reason is that the precise locations of the startup paths displayed by default on the two terminals are different.
In the default terminal of VS Code, the startup path displayed by default is the currently opened project folder. When a file is executed, it will automatically go to this file and then return to the project folder. This is convenient when executing any file in the project in this terminal, VS Code can go to it without reopening this terminal.
The "AREPL" terminal executes this python file, and it displays the parent folder of the executed file by default. When we switch other executable files, the "AREPL" terminal needs us to reopen this terminal.

How to get a file from the files on my computer to JupyterLab?

I am very new to this and struggling with the basics. I have a csv file /home/emily/Downloads/Roger-Federer.csv The textbook says that I need to "extract the file and download it to my current directory" on JupyterLab (I am using Python). What does this mean? How do I do this? Thank you
Every running program, including JupyterLab, has a "working directory" which is where it thinks it is on your computer's file system. What exactly this directory is usually depends on how you launched it (e.g., when you run a program from terminal, its working directory is initially the folder your terminal was in when you ran the command, but it's possible for a program to change its own working directory later).
Your file path indicates you're on Linux, so I'd suggest opening a terminal in your JupyterLab and running pwd to have it print out its current directory. (You can also run !pwd in any open notebook if that's easier.) You should then copy your CSV file to that directory.
If you do that, then from your Python code, you can just open the file locally, like open('Roger-Federer.csv') or pandas.read_csv('Roger-Federer.csv'). You don't have to move the file to open it from Python, though, you can just give it the entire file path, like open('/home/emily/Downloads/Roger-Federer.csv'), and that'll work just fine too.

When I try to write to a file using python script in a batch file and then run it. It doesn't write to the file

I have tried writing some content to a file by running a python code in a batch file.I have used the following as the batch command.
python C:\Python27\filtercsv.py
When I tried running the program in IDLE or in the command prompt, it was writing to the file. But, when I tried the same using the batch file, it wasn't writing.
I have included python in the PATH environment variable. So, I guess I don't need to specify the .exe file's location.
Can anyone tell me how to solve this issue?
Do you write the file to an absolute path?
If not you might want to either change the current directory in the batch file before running the script:
pushd C:\Python27
python filtercsv.py
popd
or include a path in the file name you're writing to.
This is because of if you write to a file just by name it will show up in the current working directory, which is not necessarily where the script is located.
(However, you should probably not save your scripts in your Python installation directory, nor write files there because you don't usually have write access there.)

Where does python open files to on a mac?

I made a simple python script which creates a text file. The contents of the script are
f = open("New", "w")
f.close
Now I moved the script to the desktop (I'm on a mac) and ran it, and nothing shows up. No file is created on the desktop that is visible for me.
I actually made this little script because one of my other scripts involves opening/creating a text file and reading from it. I entered the information wrong while in my program forever, and now the entire thing is broken and I cant fix it because I have no idea where it's created the darn text file. I was under the impression that just opening a file with giving it an absolute path would create it in the same directory as the script. I seem to have been mistaken.
I've been launching the script from the terminal with the command
python3 /Users/me/Desktop/script.py
Because of that, I feel like its creating the file somewhere in the python3 install location or within wherever the python3 unix exec is located. I think. Can't check.
Are any of you guys willing to help out?
-pipsqueaker117
EDIT: Here's a link to the big program which broke.
It'll be created in the current working directory, which is the directory from which you called the script.
Your file will be created in the current directory (most probably in /Users/me/ if you just opened the terminal)
Try:
cd /Users/me/Desktop/
python3 /Users/me/Desktop/script.py
You should modify your program to change the working directory (before you write the file) to the place where you want files to show up.
import os
os.chdir('/Users/me/Desktop') # or whatever
This should be a directory where you have permission to write files.
You can always ask:
import os
cwd=os.getcwd()
print(cwd)
That will print the directory the file (without a path) will be opened in.
You can change to a specific directory (in Python) this way:
import os
try:
os.chdir('/Users/me/Desktop')
except OSError as e:
print e

Categories