Spyder FileNotFoundError - python-3.x

I'm a newbie to programming. I want to switch my code from Jupyter to Spyder as I find the debugger better.
I'm trying to do something simple and open a file and I keep getting the "No such file or directory message". My text file is in the same directory as my .py file
file_palette = open(r"C:\Users\Myname.spyder-py3\palette.txt", 'r')

Related

FileNotFoundError: [Errno 2] No such file or directory for Streamlit

I am trying to make a streamlit dashboard but just to get started, I am trying to import my csv file with data. I added the csv file into the file in which I am working with on vscode which is pictured. I also coppied the path of the csv file which is
"C:\Users\solim\AppData\Roaming\.anaconda\navigator\.anaconda\navigator\scripts\FinalProject\survey.csv"
this path matches up to the path of where my python code is that I run for streamlit but once again it keeps saying that the file cannot be found even though it is exactly where my python code is I just do not understand and it is very frustrating.
Error when I run:
Proof that the file name is spelled correct and the file is in my folder:
I am expecting the file data to be printed on my streamlit page.
screenshot of launch.json code
Enter "args": ["run", "${file}"] into j.son file
Follow the steps from this tutorial to get to launch.json. After, the file should be found

Unable to read downloaded file after converting python file to .exe

I have written some code in python with the help of selenium which is doing web crawling in which it goes to a website after entering login id and password and downloaded a file. I have written the code in jupyter notebook with file name as GSPL_Code.ipynb and with the use of !jupyter nbconvert --to script GSPL_Code.ipynb and !pyinstaller GSPL_Code.py, I have first converted .ipynb file to .py and then made a .exe file from the .py file.
If I run the .exe file which is named as GSPL_Code and it is stored in dist/GSPL_Code then a file crv_report.xlsx is downloaded in the dist/GSPL_Code folder where the executable file is also there but the problem is code does not executed after downloading a file in that location. It got stuck. Nothing is happening now. When I run in jupyter notebook then I don't face any problem but after converting it into .exe, I am facing the problem.
Please find the following python code which is part of the whole script:
def handler(driver):
curr=driver.current_window_handle
for handle in driver.window_handles:
driver.switch_to.window(handle)
if handle != curr:
driver.close()
for handle in driver.window_handles:
driver.switch_to.window(handle)
print(driver.current_url)
click_event('//img[#id="IconImg_crv_report_toptoolbar_export"]')
click_event('//div[#style="white-space:nowrap;text-overflow:ellipsis;overflow:hidden;width:249px"]')
click_event('//span[#title="Microsoft Excel Workbook Data-only"]')
sleep(5)
click_event('//td[#class="wizbutton"]')
sleep(15)
print("Report is downloaded")
PATH1="C:\\Users\\ankit19.gupta\\Desktop\\Test_GSPL\\dist\\GSPL_Code"
crv_report= "/crv_report.xlsx"
print("Reading File")
dataframe1 = pd.read_excel(PATH1+crv_report)
print("Data is stored in the dataframe")
return dataframe1
I have written some print statements to see where the error is coming. So, here I am unable to see the print statement "Report is downloaded". Before this click_event('//td[#class="wizbutton"]') is used to click a button which download the file in chrome browser and I can see this downloaded file in the path dist/GSPL_Code directory and it got executed successfully but after that it got stuck and nothing happened.
I am unable to find what is happening here. Can anyone please help me. Any help would be appreciated.
Edit: There were some delay due to which print statements "Report is not downloaded" and "Reading File" were not showing in console but now I am able to see it but it is unable to read the file crv_report which is on the path PATH1 and it got stuck here and due to which I am unable to see print statement Data is stored in the dataframe
I think code is unable to access the file because os.remove is also not working when I try to delete the crv_report file. Can anyone please help me.
OS Directory Change:
When refactoring python files, in your case changing .py to .exe, it's important to make sure everything is a part of the current working directory. Simple OS checker command:
cwd = os.getcwd()
If directories are misaligned, you will need to run an OS command to change the directory before interacting with any files:
os.chdir('/your/desired/directory')

python main.py turn to text file in pycharm

I suddenly have a problem with file main.py .
I am using pycharm with python version 3.6 from anaconda.
I had a full project that run fine and now the 'main.py' became text file.
If I open a new project and name a new python file as 'main' it automatically turn into text file (any other name remains .py file).
What is the problem?

Error when Converting .py to .exe with cx_freeze

I am trying to convert my .py code to .exe. I tried all the options from posts on here and on the web, and all I could up with was the error below when I used cx_Freeze, and the .exe not opening when I used pyinstaller.
can someone help with the error below or give an alternative that works?
PS: my py code is actually ipython code from my jupyter notebook, but I'm trying not to share the code source.
Finally got it working while using pyinstaller, the key was to open the .exe file from the command window to be able to see the warning, which was "Import cv2 not found" after I deleted that line(since I was not using that module) and re built it, the .exe file now works. Hope this will help someone one day

Python idle not opening text file with open ()

being a few weeks old coder,I am unable to make python read my file despite the code and .txt file being in the same folder
highest_score=0
result_f = open("results.text")
for line in result_f:
if float (line)>highest_score:
highest_score = float(line)
result_f.close()
print("the highest score:")
print(highest_score)
and result is
Traceback (most recent call last):
File "C:\Python33\-mu.py", line 2, in <module>
result_f = open("results.text")
FileNotFoundError: [Errno 2] No such file or directory: 'results.text'
Please help
Basically you are trying to open the file result.text. The error returned is that Python Idle cannot find that file. I notice that you mentioned a .txt file but in your code you try to open a .text file.
So I would suggest you to check wheter your file extension is .txt or .text and, in case, correct.
If the error still persists, try to give the full path to the open command. For example:
result_f = open("/Users/Joe/Desktop/results.text")
Both Windows and Mac OS give, somewhere when right-clicking on the file, the full path (among other information).
I faced a similar issue, where I was trying to read a file in idle but it couldn't find the path. What worked for me:
Enter the full path of the file with the extension
Instead of the native path writing style in windows i.e. (D:\python\sample.txt)
use (D:/python/sample.txt)
It worked out for me in Windows 7, IDLE 3.6.4 and also works on Windows 8

Resources