Python Forum
PermissionError while running function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PermissionError while running function
#11
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.
Reply
#12
Hey! I changed the code, it still gives the error with this:

Checking that n_files divided by n_pts is an integer...
Done!
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\CeNO3.py", 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\Functions.py", line 134, in sum_import_data_2
    mcadata[:, energy_point] += np.loadtxt(filename)

  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: '\\\\...\\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.
Reply
#13
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?
Reply
#14
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('Done!')
    else:
        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))
    print('Done!')
    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!
Reply
#15
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)))
Reply
#16
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.
Reply
#17
mca_file has a wrong value. There should be a {} group for the call to format(). It should pobably be something like
'\\\\...\\home\\s\\myacc\\Documents\\XAS\\mca_CeNO3_I1\\{}'
Without the {} group, the mca is not inserted in the path.
Reply
#18
Thanks! I tried the one you suggested, as well as
...\\XAS\\mca_CeNO3_I1\\mca_{:05}
Both give this error:

  File "\\...\home\s\myacc\Documents\Python Scripts\Functions.py", 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.
Reply
#19
I see the problem, replace the line
filename = mca_file.format(mca)
with
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?
Reply
#20
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  the order of running code in a decorator function akbarza 2 477 Nov-10-2023, 08:09 AM
Last Post: akbarza
  Class function is not running at all even though it is clearly run in the code SheeppOSU 2 2,034 Sep-26-2020, 10:54 PM
Last Post: SheeppOSU
  Fixing "PermissionError: [Errno 13] Permission denied" puredata 17 71,933 Mar-09-2020, 03:20 PM
Last Post: syssy
  Running function from parent module which has a loop in it. ta2909i 1 2,652 Nov-18-2019, 07:04 PM
Last Post: Gribouillis
  PermissionError: [Errno 13] Permission denied: error leviathan54 2 46,566 Apr-20-2019, 12:51 AM
Last Post: leviathan54
  [python] PermissionError: [Errno 13] Permission denied akamouch 1 42,392 Feb-07-2019, 03:28 PM
Last Post: ichabod801
  PermissionError Quavin 1 2,740 Dec-02-2017, 10:34 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020