Python Forum
PermissionError while running function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: PermissionError while running function (/thread-28689.html)

Pages: 1 2


PermissionError while running function - Laplace12 - Jul-30-2020

I need help understanding an error. I have created a function that's supposed to read all files in a specific folder and sum them. However, there is a PermissionError when trying to execute the code, and as I understand this comes if you're trying to write to that folder and don't have permission; but I'm not trying to create any files in that folder, simply read the files in there and sum them! So even if the error itself is not a python issue, there is then something strange about the function that I don't understand because it shouldn't be creating any files. The function is this:

def function(n_pts, n_chnl, mca_file):
    mcadata = np.zeros([n_chnl, n_pts])

    for energy_point in range(n_pts):
        current_mca = os.listdir(mca_file)

        for mca in current_mca:
            mcadata[:, energy_point] += np.loadtxt(mca_file.format(mca))
    print('Done!')
    return mcadata
I have defined mca_file, which is the location of the folder in my computer.

PermissionError: [Errno 13] Permission denied: 'mca_file'
So, is the function doing something it shouldn't? Listdir command is used to make a list of all the files that should be summed up, and then mcadata is defined as the sum.


RE: PermissionError while running function - Gribouillis - Jul-30-2020

Please could you post the entire error traceback and not only the error message on the last line. The whole traceback contains informative debugging information.


RE: PermissionError while running function - bowlofred - Jul-30-2020

(Jul-30-2020, 01:27 PM)Laplace12 Wrote: as I understand this comes if you're trying to write to that folder and don't have permission

Writes aren't required. It can happen if you're trying to open/read a file or a directory and you don't have read permission.


RE: PermissionError while running function - Laplace12 - Jul-30-2020

Hey, here is the entire traceback. Good to know that writes aren't necessarily required: I simply wish for the function to read files and sum, which I suppose it should be doing now.

Traceback (most recent call last):

  File "\\...\myacc\Documents\Python Scripts\CeNO3.py", line 45, in <module>
    I1_data = fn.sum_import_data_2(n_pts, n_chnl, I1_mca_file)

  File "\\...\myacc\Documents\Python Scripts\Functions.py", line 136, in sum_import_data_2
    mcadata[:, energy_point] += np.loadtxt(mca_file.format(mca))

  File "C:\LocalData\myacc\Anaconda\lib\site-packages\numpy\lib\npyio.py", line 981, in loadtxt
    fh = np.lib._datasource.open(fname, 'rt', encoding=encoding)

  File "C:\LocalData\myacc\Anaconda\lib\site-packages\numpy\lib\_datasource.py", line 269, in open
    return ds.open(path, mode, encoding=encoding, newline=newline)

  File "C:\LocalData\myacc\Anaconda\lib\site-packages\numpy\lib\_datasource.py", line 621, in open
    encoding=encoding, newline=newline)

PermissionError: [Errno 13] Permission denied: '(mca file here)'



RE: PermissionError while running function - Gribouillis - Jul-30-2020

The traceback seems to indicate that you are trying to open a file named (mca file here). I guess that there is no such file on the file system.


RE: PermissionError while running function - Laplace12 - Jul-31-2020

Strange: what is defined as "mca_file" is the folder in which the files are in, not a file. So I would need for the code to list the files in that folder, the naming is a bit confusing. Is the listdir command unfit for this job?


RE: PermissionError while running function - Gribouillis - Jul-31-2020

File or directory, the problem is not with the functions that you are using. Where does that (mca file here) come from? That's what you have to find in the code. Normally there should be no problem debugging this: find the exact file or directory that the code is trying to access, check if it exists, if it has the appropriate permissions and that's all. Raise exceptions if necessary or print logs.


RE: PermissionError while running function - Laplace12 - Jul-31-2020

Okay. I am working with Windows and checked the rights on that folder: my account is the Owner and should have Full control. The folder is simply one I've created and put all the mca files in, and the path should definitely be correct. In another function which relies on a certain file number, the path is ''\\\\...\\home\\s\\myacc\\Documents\\XAS\\mca_CeNO3\\mca_{:05}' and the reading works just fine, with this function I'm trying to read the whole directory, so it's simply ''\\\\...\\home\\s\\myacc\\Documents\\XAS\\mca_CeNO3' and there's an error, nothing else changed.


RE: PermissionError while running function - Gribouillis - Jul-31-2020

Could you modify this part in Functions.py
mcadata[:, energy_point] += np.loadtxt(mca_file.format(mca))
Use for example this to check which files are used and if they actually exist
filename = mca_file.format(mca)
print('DEBUG: is file? ', os.path.isfile(filename), filename)
mcadata[:, energy_point] += np.loadtxt(filename)



RE: PermissionError while running function - anne - Jul-31-2020

As a new user of python I cannot comment on the code , but
in other language when file is open ( file.open) it will be created if it does not exists.
If python has similar concept - is checking for existence of file necessary ?