causes of Python IOError: [Errno 13] Permission denied

2024/10/5 19:47:45

When attempting to write a file, I can get this same error when any of following conditions applies:

  1. The file exists and is marked read-only.

  2. I don't have write permission for the folder and therefore cannot create a file.

  3. The file is already open in another process.

Is there any way to separate out the above three causes? If not, then I would say that this is an extraordinarily poor design.

Answer

You get the same Exception as your base problem is "You can't do this" but the details are contained in the exception instance, (or in a string on older pythons), so if you handle the problem as:

try:outfile = open('somename.txt', 'w')
except IOError, e:print "Not allowed", e

You will get a lot more information. (N.B. if you are running python 3 you will need to add brackets to the print above).

This is actually incredibly good design - your basic problem is that there is a problem - and you can find out more if you care to - and you can handle a given class of problem in a generalised manner.

https://en.xdnf.cn/q/119865.html

Related Q&A

Python Invalid Syntax IF statement

Im trying to make a quiz in python but I keep getting invalid syntax errors.#This is for addition questions.if (question=add) <---- That is where i get the error for i in range(0,10):first_number_a…

record a web page using python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 3…

returning a string from askopenfilename() to a entry box

I have seen many postings on the use of askopenfilename(), however I still cant seem to find anything to help me display the full file path in an entry box once I have selected said file. below I have…

Overflow error in Python program

Please help me to understand why this code doesnt work. I know there is something very stupid wrong. This should be an implementation of the fourth order Runge kutta algorithm to solve Lorentz system o…

python login 163 mail server

When I use this script to login the 163 mail server,there is something wrong! My python env is python 2.7.8 Please help me!import imaplibdef open_connect(verbose=False):host = imap.163.comport = 993if …

How to get list of keys that share a value with another key within the same dictionary?

I have a dictionary of unique keys where some keys share the same value. For example:D = {ida:{key:1},idb:{key:2},idc:{key:3},idd:{key:3},ide:{key:4},idf:{key:4},idg:{key:4}}I want a list of keys that…

Sqlite3 Error: near question mark: syntax error [duplicate]

This question already has answers here:Parameterized query binding table name as parameter gives error(3 answers)How to use variables in SQL statement in Python?(5 answers)Closed last year.I am trying…

running bs4 scraper needs to be redefined to enrich the dataset - some issues

got a bs4 scraper that works with selenium - see far below: well - it works fine so far: see far below my approach to fetch some data form the given page: clutch.co/il/it-services To enrich the scrap…

Uploading a file in a embed discord.py (not a image)

Im trying to upload a file directly in a embed, I can upload the file but I dont find the way to put it in the embed. What I want is not displaying the file but uploading it so we can download it, is i…

Cannot install psycopg2 on virtualenv

Hi I use manjaro Linux and I tryed to install psycopg2 packge inside virtualenv but it gave errror error: command gcc failed with exit status 1. Then in the console I tryed gcc --version it saidbash: …