How to run py file that uses txt file in Jupyter Notebook - python

I'm trying to run a python file through Jupyter Notebook. The first part of this file asks the user to import the data (.txt) file. Running it on a python IDE works well, however I want to make it work in Jupyter Notebook. I tried uploading the data beforehand, as seen in the image below.
It still doesn't detect the file nor reads the data. Any ideas on what else to try?

your jupyter has a directory where the notebooks are saved. If the file is not inside the notebook working directory (or a subtree of it) you can't access it, because it not exists. Your path C:\User\Downloads is obvisouly not this directory. Set your download directory to a jupyter directory, or move your text file. Check in jupyter what your working directory is.
Path errors are common in python. Especially if you often have to switch the programming language and then, few weeks later, back to python. I started to make me a helper method that I use in general in every new notebook and python application, to make not path errors. Maybe it helps you with correct working directory paths in the future.
import os
import sys
from pathlib import Path
def SetWorkingDir(abs_working_dir_path):
Debug = True
try:
if Debug:
print(f"[SetWorkingDir] Initial working directory: {os.getcwd()}", flush=True)
RootDir = False
if RootDir:
root_dir = os.path.dirname(os.path.realpath(__file__))
if Debug:
print(f"[SetWorkingDir] Local dir: {root_dir}", flush=True)
if (os.getcwd() != abs_working_dir_path):
os.chdir(abs_working_dir_path)
if Debug:
print(f"[SetWorkingDir] Working directory changed to: {os.getcwd()}", flush=True)
else:
if Debug:
print(f"[SetWorkingDir] Working directory unchanged at: {os.getcwd()}", flush=True)
return os.getcwd()
except Exception as ex:
print(f"\n[SetWorkingDir] Exception:"
f"\n{'Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(ex).__name__, ex}\n", flush=True)
And then all further paths relative to youur so set working directory (to make sure that your code also runs on other systems thans yours. Also use os.sep instead of \ as path seperator.)
best greetings

Related

Running python file through VS Code terminal

My question is I am unable to run a python file in VS Code terminal unless I specify the full path.
Whereas, when I see any python tutorial, only python filename is entered and it works.
Can somebody pls help me with this issue?
The files need to be in the same folder where you are operating / or have the other Python files.
You can use two ways for this:
first:
import os
pwd = os.getcwd()
# again, make sure you file is in the same directory.
the_file = (pwd + "\\filename.xlsx")
or Secondly,
#the below you can use wherever your file is and it will locate it.
# you specify the full path using pathlib.path and:
pathlib.path(directory here without the file itself).joinpath(file name here).

Python/Jupyter getting a FileNotFoundError when attempting to read an excel file however said file is in the correct directory

data = pd.read_excel("ETH-USD")
I continually receive an error message informing me that the file cannot be found
this is despite the fact that
1: my directory has been changed within to Python to the same address as the folder where the excel file is stored
2: the file name is input exactly as displayed
Any ideas on why it is unable to find my file?
Is it possible that the excel file has an extension of .xlsx, but your file explorer is set to "hide file extensions"? Try:
data = pd.read_excel("ETH-USD.xlsx")
Or, see what's in the current directory by running:
import os
print(os.listdir())
A tip from the comments:
Windows tip too: hold Shift, right click the excel file and copy as path, then you can see its path (if you don't enable viewing file extensions in the file browser). –
creanion
Often when running python scripts from compilers the "working directory", or where you are running the script from doesn't match the location of your script, hence why I find it much more reliable to use this instead:
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
data = pd.read_excel(os.path.join(BASE_DIR,"ETH-USD")
To add, while I do not use Jupyter, in VSCode which I use, the working directory (which is where python looks for if you put a path in read_excel() but its not a full path) is often the current directory opened in there, so I expect a similar issue to be the reason for your issue.

'No such file or directory' error when using nosetests

I have a little Python project of which I recently made a Conda package. Making the package was a pain on its own, however, I recently started adding tests to this using nosetests, which made it even harder.
To introduce: my package takes a certain input, performs a lot of (quantum chemical) calculations and then generates a folder in the same directory as the script which calls the package, containing the output data (some .png files, .txt files and binary files)
Using nosetests, I would like to check whether these output files are how they should be. I created a Python test script (using unittest) which creates the input and calls my package. Next, it imports the created file and the test file. However, this is where it goes wrong. I get the error that this file does not exist:
FileNotFoundError: [Errno 2] No such file or directory: 'results\\output.txt'
The directory looks like this:
project-path\
- tests\
- test-script.py
- results\
- output.txt
I call nose by running this in Anaconda prompt:
project-path> nosetests tests
And I import the file in the Python test script using:
result_file = open('results\\output.txt', 'r').read()
Does anyone know what goes wrong here? I think it has to do with the fact that the tests are executed in a test environment. In that case: how do I import my files?
Get the absolute path to output.txt, it is indeed the most reliable way to locate and open it.
import os, sys
basedir = os.path.dirname(sys.argv[0])
filename = "output.txt"
path = os.path.join(basedir, "results", filename)
result_file = open(path, 'r').read()

Python unable to move directories using shutil

I'm trying to write a script where it will move a directory (containing images to be processed) into another dedicated working directory (within the project folder) by using shutil.move().
However, my Python script keeps on failing to achieve this by throwing an [Errno 2] No such file or directory: '/Users/user/Desktop/Captured\\ Images' exception.
The target directory is being perfectly recognised so that's not the issue.
I'm currently using macOS to develop this script, and using standard BASH.
This is how I input the path of the source directory (by dragging the folder into the terminal): /Users/user/Desktop/Captured\ Images.
This is how Python interprets the source directory: /Users/user/Desktop/Captured\\ Images.
The script works perfectly when I modify the source path to: /Users/user/Desktop/Captured Images.
I've also tried using the Pathlib Module to prevent file separator issues, but that also didn't work.
I know for sure that what's causing the problem is the \ within the file path since the folder is named 'Captured Images' with a space between it, thus Captured\ Images.
Here's the following source code:
move_directory(input("Please Input The Directory Of The Captured Images You Would Like To Import: "))
def move_directory(source_directory):
try:
shutil.move(source_directory, os.path.dirname(os.path.dirname(__file__)) + '/Captured Images/Source Images')
except OSError as errorMessage:
print("Failed To Move Directory: {0}".format(errorMessage))
Update 1:
I've just tried to move the directories by using subprocess.run(["mv", ...]) and it worked, so now I see that it's not how the script is taking in the input, but a problem between the path input and shutil.move().

Python command for setting working directory to source file location in Spyder

I want a python line of code that sets the working directory to folder the code is part of. I am using spyder IDE for writing and running python codes.
Side note: This question is very similar to R command for setting working directory to source file location in Rstudio
This is a common problem I run into when developing in Jupyter for the command line.
You can try this to find where your script is executing from:
import os
from pathlib import Path
def myPath():
'''
return the current working directory in both interpeters and when exectued on the commandline
'''
try:
# path of this file when executed
wd = os.path.dirname(os.path.abspath(__file__))
except NameError as e:
print('this script is running in an interpreter')
# if not found
wd = Path().resolve()
return(wd)

Categories