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.
Related
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.
I have an python program that scans all files in a directory, but when I want to open a file with no extension like C:\path\to\file\actual file, then python gives me a permission denied error.How can I fix that?
What I mean with no extension is that it does not have .exe or .txt or something like that.How can I read files without adding extensions to the files manually?
This is the full error:
Traceback (most recent call last):
File "scan.py", line 152, in <module>
file = open(i,'r')
PermissionError: [Errno 13] Permission denied: 'C:\\path\\to\\file\\my_file'
Ok, I have zero idea why it does not work other than what my comment stated, however this program will work for windows.
import os
s = "H:\path\to\file\ActualFile"
for subdir, dirs, files in os.walk(s):
for file in files:
try:
GatheredFiles = (os.path.join(subdir, file))
except PermissionError:
print("You do not have permision to go in here"))
break
Edit: added the try statement
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\.
I am trying to write a python script to copy some files from one place to another like this:
os.system('cp file/path/a file/path/b')
when I run this within the python prompt it works just fine. But if I put this in a python script it fails with the error
cp: cannot create regular file 'file/path/b': Permission denied
I am getting this error even when I run the script using sudo permissions i.e. sudo ./mysript.py
So what is going on here and is it possible for me to fix this?
EDIT 1:
Following the suggestions of #kungphu and #ShadowRanger I switched from using os.system to using shutil.copy. The problem persists however I am now getting a different error:
Traceback (most recent call last):
File "./myscript.py", line 21, in <module>
shutil.copy('/absolute/file/path/a', '/absolute/file/path/b')
File "/python/lib/python2.6/shutil.py", line 84, in copy
copyfile(src, dst)
File "/python/lib/python2.6/shutil.py", line 50, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 13] Permission denied: '/absolute/file/path/a'
I have a python program which inputs a .wav file name from the user, does some filtering and writes the output to another .wav file. The programs works perfectly well when I run it using the interpreter. But when I run the program using command prompt, it prompts for the input audio filename, but when I type the .wav filename and press enter, I get the following error:
Traceback (most recent call last):
File "C:\python_audio\stereo.py", line 15, in <module>
(fs, x) = read(audio_in,'rb')
File "C:\Python27\Lib\site-packages\scipy\io\wavfile.py", line 155, in read
fid = open(filename, 'rb')
IOError: [Errno 2] No such file or directory: 'chostakovitch.wav'
The python program stereo.py and the .wav file (chostakovitch.wav) are in the path C:\python_audio, and I have added this path as an environment variable.
But I can't figure out why I am still getting this error?