Python DataFrame to_csv Errno 13 Permission denied - python

I'm working on Windows 10, Python code is executed in Spyder (Python 3.7).
Writing does not work in my main file folder (D:/main_file_folder), however it works on the higher level (D:) or just created folders (D:/just_created).
I tried to open Spyder with administrator privileges.
I tried to remove writing protection in the destination folder properties, but the writing protection tick returns eventually, once I check the properties again.
I tried to give the user all privileges in the folder properties security tab. It sticks, but has no effect.
dest = Path('D:/anyfolder/file.csv')
some_pandas_df.to_csv(dest, encoding="utf-8")
File "C:\Anaconda\lib\site-packages\pandas\io\common.py", line 428, in get_handle
f = open(path_or_buf, mode, encoding=encoding, newline="")
PermissionError: [Errno 13] Permission denied: 'D:\main_file_folder\file.csv'

Related

Permissions Error and No such file or Directory

if not os.path.exists('/var/log/'):
os.makedirs('/var/log/')
print(log_filepath)
os.chmod(log_filepath, stat.S_IWOTH)
f_log_in = open(log_filepath, "a")
Without the chmod command, it will throw an error saying permission denied for the f_log_in file open command.
f_log_in = open(log_filepath, "a")
PermissionError: [Errno 13] Permission denied: '/var/log/s3_sync.log'
When I include the os.chmod command, it says:
os.chmod(log_filepath, stat.S_IWOTH)
FileNotFoundError: [Errno 2] No such file or directory: '/var/log/s3_sync.log'
Are there any other ways of approaching this?
EDIT: THIS IS NOT A DUPLICATE, I DELETED THE OTHER ONE.
The file indicated in log_filepath doesn't exist. Thus, you cannot simply open it, as you do with open. See this answer for more info, but you need w+ or a+ to also create the file.
The second error you get is because of exactly the message - the file doesn't exist, so you can't change the permissions to write to it.
Now, you might still have a problem if you are not executing the program as a user with sufficient permissions to access /var/log (or wherever log_filepath points to). You have to run as a user with sufficient permission - there is no way around that, either by running as a user with that permission or by changing the permissions of the directory itself so that the user you are executing as would have sufficient permission.

Cannot write to a file within python script, on startup

Basically I have a python script which is "converted" to exe, located in C:\Users\USER_NAME\AppData\Roaming\Folder. When started manually (not as admin), the script can write to file (file is located in the same directory as the script). But when the script runs on startup (I added a registry string to Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run with an executable path as data) I get Permission Denied error, why is that so and how could I fix it?. Code for opening file:
file = open(os.path.join(current_path, "text.txt"), "a+")
Error:
PermissionError: [Errno 13] Permission denied: '.\\text.txt'
The issue was that the script was run from C:\Windows\System32 not from C:\Users\USER_NAME\AppData\Roaming\Folder (windows being weird I guess)

PermissionError on writable file in Jenkins

I'm trying to open a file in python that was opening fine before with this syntax, but now that I've rearranged directories I am getting a PermissionError and I cannot pinpoint why. I have directory ahs_reports containing files create_ahs_report.py and message_body.html. ahs_report is contained in a directory called reports. message_body.html has permissions -rwxrwxrwx so it should be writable. ahs_report/ also has the same permissions. However, when I run create_ahs_report.py from directory reports, and get to this line
with open('message_body.html', 'w+') as f:
I get PermissionError: [Errno 13] Permission denied: 'message_body.html'. I feel like there's something obvious I am missing. What is it? I am running the script thru a Jenkins job as user jenkins and I've never had an issue like this.

Python 2.7; Downloaded a file and attempted to overwrite an .mp3, receiving: IOError: [Errno 13] Permission denied: 'audio.mp3'

Python 2.7
Windows 8
I downloaded a file from the internet using:
testfile = urllib.URLopener()
testfile.retrieve(str(audio_link), "audio.mp3")
audio_link is a format something like:
http://static.sfdict.com/staticrep/dictaudio/R02/R0203700.mp3
The program should be writing to an existing audio.mp3 in the working directory with the url from the above code. It was working properly until I made some changes to the program that don't seem to have to do with the problem.
Specifically, the problem shows:
File "D:\Paul\projects\spelling test\python.py", line 105, in get_pronunciation
testfile.retrieve(str(audio_link), "audio.mp3")#open the link as a saved file; creates file; maybe overwrites old??
File "C:\Python27\lib\urllib.py", line 249, in retrieve
tfp = open(filename, 'wb')
IOError: [Errno 13] Permission denied: 'audio.mp3'
get_pronunciation is a function, and I think that tfp is built in to the testfile usage.
My cmd has no chmod (I tried chmod 777 /working/directory/blah/) and, even as administrator, it is not recognized as a command.
I also tried to go to the properties of the working directory through windows explorer>right-click>properties>security tab>edit>click on Users>check-mark full control.
Help? Regards.

celery log shows "Permission Denied"

I'm running celery on Windows, and there's a task that writes a file on a network drive. Looks like it's having issue to write due to permission. Here's part of celery log that shows the error:
File "C:\TaskerApp\FlaskApps\Tasker\tasks.py", line 194, in export
with open(filename, 'wb') as w:
IOError: [Errno 13] Permission denied: u'//saab/Data/5863/5-Message/5863_2M.txt'
And this is the original code:
#celery.task(name="tasks.export")
def export(file_name):
with open(file_name, 'wb') as w:
w.write('test')
However when I try to write the file directly from python command line, I don't see any issue.
Just wonder what is the possible reason to cause this issue?
Finally I got this work. Looks like the service is running under Local System Account, after I share the file path with this local account, the issue went away.

Categories