Posts: 4,790
Threads: 76
Joined: Jan 2018
Jul-31-2020, 05:57 PM
(This post was last modified: Jul-31-2020, 05:57 PM by Gribouillis.)
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.
Posts: 49
Threads: 19
Joined: Jul 2020
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.
Posts: 4,790
Threads: 76
Joined: Jan 2018
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?
Posts: 49
Threads: 19
Joined: Jul 2020
Aug-01-2020, 04:14 PM
(This post was last modified: Aug-01-2020, 04:15 PM by Laplace12.)
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!
Posts: 4,790
Threads: 76
Joined: Jan 2018
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)))
Posts: 49
Threads: 19
Joined: Jul 2020
Aug-02-2020, 08:16 AM
(This post was last modified: Aug-02-2020, 08:16 AM by Laplace12.)
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.
Posts: 4,790
Threads: 76
Joined: Jan 2018
Aug-02-2020, 09:28 AM
(This post was last modified: Aug-02-2020, 09:28 AM by Gribouillis.)
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.
Posts: 49
Threads: 19
Joined: Jul 2020
Aug-02-2020, 09:48 AM
(This post was last modified: Aug-02-2020, 09:49 AM by Laplace12.)
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.
Posts: 4,790
Threads: 76
Joined: Jan 2018
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?
Posts: 49
Threads: 19
Joined: Jul 2020
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!
|