python in notepad++ no such file or directory - python

python 2.7. Notepad++. Windows 10
I'm trying to open file like this:
sourcefile=raw_input('name of the file: ')
fhand=open(sourcefile,'r')
And it gives me this error:
Traceback (most recent call last):
File "C:\Users\Denis\Desktop\prog\python\es.py", line 135, in <module>
fhand=open(sourcefile)
IOError: [Errno 2] No such file or directory: '20.04.2016.csv'
In notepad++ i run this: C:\Python27\python.exe -i "$(FULL_CURRENT_PATH)"
Kinda new to programming but i'm guesing that I have to specify somehow path to the place where my es.py and my file is. I thought $(FULL_CURRENT_PATH) does it.
If i run my code with python or idle it works fine.
I've tried to run this program in Atom, gives same mistake. No mistakes so long as I don't work with files.

Related

opening a .txt file in same folder as .py not working

My vscode is having problems using the open() function to open a file.
I am trying to open a .txt file. Both the py file and .txt file are in the same folder (named "practice"), but python terminal is erroring out with
File "c:\Users\liamt\OneDrive\Desktop\VSCode\Practice\p2.py", line 4, in <module>
fin = open('ex_text.txt')
FileNotFoundError: [Errno 2] No such file or directory: 'ex_text.txt'
PS C:\Users\liamt\OneDrive\Desktop\VSCode> python -
I tried using the relative path to the .txt file but a permission error came up.
Traceback (most recent call last):
File "c:\Users\liamt\OneDrive\Desktop\VSCode\Practice\p2.py", line 4, in <module>
fin = open('C:\\Users\\liamt\\OneDrive\\Desktop\\VSCode\\Practice')
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\liamt\\OneDrive\\Desktop\\VSCode\\Practice'
PS C:\Users\liamt\OneDrive\Desktop\VSCode>
What am I doing wrong? Really appreciate any help.
When using vscode, open the entire folder/directory containing all the files by using file > open folder, and not just double clicking the python file alone. By opening the entire folder, it is included in the workspace and can be opened by the python file that exists along with it.

cannot write to file if I run .exe

I am writing a short program where I want to write an array full of street names to a .txt file. Everyting works fine, but if I convert the python code to a .exe with pyinstaller, I cant write to a new .txt file anymore. Why does this happen?
with open(f"C:\\Users\\auser\\Desktop\\tset\\{txtname}.txt", "a") as txt_file:
for line in new_addresses:
txt_file.write(line + "\n")
txt_file.close()
this is how I create the .exe:
pyinstaller --onefile streets.py
Traceback (most recent call last):
File "streets.py", line 65, in
PermissionError: [Errno 13] Permission denied: 'C:\Users\auser\Desktop\tset\test.txt'
[7048] Failed to execute script rewriteword
I got this to work now. My anti virus program was blocking for some reason the execution.

How to make vscode work with playsound module?

I have this very simple test code:
from playsound import playsound
playsound('alarmsound.wav')
alarmsound.wav is a file in the same directory. when I run it in a terminal, it works fine. But when I run it in VSCode, it blows up with the following message:
Traceback (most recent call last):
File "c:/Users/Luca/Desktop/Python/own/playaudio.py", line 3, in <module>
playsound('alarmsound.wav')
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38-32\lib\site-packages\playsound.py", line 35, in _playsoundWin
winCommand('open "' + sound + '" alias', alias)
File "C:\Users\Luca\AppData\Local\Programs\Python\Python38-32\lib\site-packages\playsound.py", line 31, in winCommand
raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
Error 275 for command:
open "alarmsound.wav" alias playsound_0.7197283376226026
Cannot find the specified file. Make sure the path and filename are correct.
It used to work in vscode before, now suddenly it blows up. what could be wrong?
Make sure you're actually running the file from the directory where your sound file is in.
In my case, it was running Python from the parent directory of the folder with the .wav file.

Pyinstaller Error Bundling Egg Files FileNotFoundError: [Errno 2] No such file or directory

I am attempting to convert my python file (.py) into a Windows executable (.exe). To do this, I am using Pyinstaller. My python includes the module PyBluez for Bluetooth capabilities. When I run the command pyinstaller <path> -F from the directory of the py file, it gives an error:
FileNotFoundError: [Errno 2] No such file or directory: 'c:\users\X\appdata\local\programs\python\python37-32\lib\site-packages\pybluez-0.22-py3.7-win32.egg\bluetooth\widcomm.py'
After quite a bit of research, I am unable to figure out what is wrong with my program. Do I need to specifically specify where to find the egg file? If so, How would I do that? Thank you! Also, I tried to use cx_Freeze and it also gave an error about the bluetooth module:
Traceback (most recent call last):
File "C:\Users\X\AppData\Local\Programs\Python\Python37\lib\site-packages\cx_Freeze\initscripts__startup__.py", line 14, in run
module.run()
File "C:\Users\X\AppData\Local\Programs\Python\Python37\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
exec(code, m.dict)
File "BuzzerBeater.py", line 3, in
ModuleNotFoundError: No module named 'bluetooth'
I had the same problem, and managed to solve it by unzipping the .egg file to a folder with the same name (including .egg in the folder name), in my case C:\Users\X\AppData\Local\Programs\Python\Python37\Lib\site-packages\PyBluez-0.22-py3.7-win-amd64.egg\.

File not found error, but file exists

I am working on a simple webapp from Head First Python. I am using ubuntu 14.04. But it is the error:
Traceback (most recent call last):
File "/usr/lib/python3.4/http/server.py", line 1143, in run_cgi
os.execve(scriptfile, args, env)
FileNotFoundError: [Errno 2] No such file or directory:
'/home/sarthak/Desktop/Learn/HeadFirstPython/chapter7/webapp/cgi-bin/generate_list.py'
127.0.0.1 - - [12/Jun/2015 07:08:25] CGI script exit status 0x7f00
The file surely exists on the system and it is made executable.
How to fix the problem?
The Problem lies in the 'wrong' path of the shebang line (the first line which says #!/usr/local/bin/python3) of the generate_list.py file.
If you are using ubuntu the location of python3 is probably /usr/bin/python3 (You can find out your python3 location by typing which python3 into the shell/terminal). So you have to adjust the first line to the actual address of python3 in order to make your script work.

Categories

Resources