Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 PermissionError while running function
Normally, in any language, if one attempts to open a file for reading and the file doesn't exist there is an error
>>> open('spam.txt')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'spam.txt'
The problem in your case is to investigate the cause of the error, that's why I suggest that you print a maximum information about the file to be opened before the error occurs.

The solution of bugs doesn't come out of the blue, it comes from exposing the data used by the program at run time.
Hey! I changed the code, it still gives the error with this:

Checking that n_files divided by n_pts is an integer...
Summing up mcas in mca_file...
DEBUG: is file?  False \\...\home\s\myacc\Documents\XAS\mca_CeNO3_I1
Traceback (most recent call last):

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

  File "\\...\home\s\myacc\Documents\Python Scripts\", line 134, in sum_import_data_2
    mcadata[:, energy_point] += np.loadtxt(filename)

  File "C:\LocalData\myacc\Anaconda\lib\site-packages\numpy\lib\", line 981, in loadtxt
    fh =, 'rt', encoding=encoding)

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

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

PermissionError: [Errno 13] Permission denied: '\\\\...\\home\\s\\myacc\\Documents\\XAS\\mca_CeNO3_I1'
What's strange is that previously I ask the function to calculate the files in the mca_file folder, and that works just fine and the number of files is correct, but somehow for that part of the function the repository can't be found.
Laplace12 Wrote:DEBUG: is file? False \\...\home\s\myacc\Documents\XAS\mca_CeNO3_I1
Now we see that your code tries to read this path which is not a file on your system. You should be able to say what's the error now. Either this file should exist and for some reason it is not there, either the code computed the wrong filename. What do you think?
Well, it's not a file but a directory, but as I understand that shouldn't make a difference? The directory does exist, and the path should be correct as it works when calculating the files in that directory in the same function, and I use the same mca_file there. The complete function is this

def function(n_pts, n_chnl, mca_file):
    print('Checking that n_files divided by n_pts is an integer...')
    n_files = 0
    for path in pathlib.Path(mca_file).iterdir():
        if path.is_file():
           n_files += 1
    if n_files % n_pts == 0:
        print('Not an integer, check the number of files in your mca folder')
        raise SystemError
    print('Summing up mcas in 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:
            filename = mca_file.format(mca)
            print('DEBUG: is file? ', os.path.isfile(filename), filename)
            mcadata[:, energy_point] += np.loadtxt(filename)
            #mcadata[:, energy_point] += np.loadtxt(mca_file.format(mca))
    return mcadata
and the code does the first part well, dividing n_files with n_pts using the same exact directory named as 'mca_file'. Perhaps the os.listdir command doesn't work here, then? Also, huge thanks for your help so far!
If it is a directory, then it is an error to use np.loadtxt() with it. You need to print more information, use
            print('DEBUG: is file? ', os.path.isfile(filename), filename)
            print('DEBUG: mca: {}, mca_file: {}'.format(repr(mca), repr(mca_file)))
DEBUG: is file?  False \\...\home\s\myacc\Documents\XAS\mca_CeNO3_I1
DEBUG: mca: 'mca_01066', mca_file: '\\\\...\\home\\s\\myacc\\Documents\\XAS\\mca_CeNO3_I1'
Now it notes an mca file, mca_01065 is the first file in the given directory.
mca_file has a wrong value. There should be a {} group for the call to format(). It should pobably be something like
Without the {} group, the mca is not inserted in the path.
Thanks! I tried the one you suggested, as well as
Both give this error:

  File "\\...\home\s\myacc\Documents\Python Scripts\", line 129, in sum_import_data_2
    current_mca = os.listdir(mca_file)

FileNotFoundError: [WinError 3] The system cannot find the path specified: '\\\\...\\home\\s\\myacc\\Documents\\XAS\\mca_CeNO3_I1\\mca_{:05}'
, so I'm guessing listdir doesn't work properly with this definition of mca_file and I should switch to another command.
I see the problem, replace the line
filename = mca_file.format(mca)
filename = os.path.join(mca_file, mca)
Then use only the folder name when you call the function, forget about the mca_{:05}.

Do you understand these path manipulations?
Hey, yes, now I get it: as the two paths are joined it reads the folder in listdir which works for directories, and then proceeds to read files (mca) with loadtxt that works for files. Thank you so much for your help!

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Multiproccessing ValueError: Pool not running when running parallel functions Gilush 1 256 Jun-06-2020, 12:49 AM
Last Post: Gilush
  Fixing "PermissionError: [Errno 13] Permission denied" puredata 17 9,407 Mar-09-2020, 03:20 PM
Last Post: syssy
  Running function from parent module which has a loop in it. ta2909i 1 357 Nov-18-2019, 07:04 PM
Last Post: Gribouillis
  PermissionError: [Errno 13] Permission denied: error leviathan54 2 24,522 Apr-20-2019, 12:51 AM
Last Post: leviathan54
  [python] PermissionError: [Errno 13] Permission denied akamouch 1 33,948 Feb-07-2019, 03:28 PM
Last Post: ichabod801
  PermissionError Quavin 1 1,098 Dec-02-2017, 10:34 PM
Last Post: snippsat

Forum Jump:

Users browsing this thread: 1 Guest(s)