cannot write to file if I run .exe - python

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.

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.

Permission error without a file extension

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

Python script requires permission to copy files

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'

python in notepad++ no such file or directory

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.

Python IOError: No such file or directory

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?

Categories

Resources