python call error module, report file not found

Problem Description

Here Insert Picture Description
Here Insert Picture DescriptionCall readData modules other modules, can not find a file found. If the module directly through readData, there would be no problem. I am very interested in this issue, so now we begin to solve this problem, right?

Solutions

Through the above description of the problem, we can know the path to the file without problems. Just call the procedure, it appears for some reason can not find the path. Let's take a look at the error message:

C:\Users\lenovo\AppData\Local\Programs\Python\Python37\python.exe D:/360MoveData/Users/lenovo/Desktop/startProject/OAS.Cloud.PAAS_Interface/TestCases/UM/Test_01_register.py
Traceback (most recent call last):
  File "D:/360MoveData/Users/lenovo/Desktop/startProject/OAS.Cloud.PAAS_Interface/TestCases/UM/Test_01_register.py", line 70, in <module>
    path = readCase.readCasedata("UM", "Test_01").getPath()
  File "D:\360MoveData\Users\lenovo\Desktop\startProject\OAS.Cloud.PAAS_Interface\common\readCase.py", line 28, in getPath
    row = self.getCase()
  File "D:\360MoveData\Users\lenovo\Desktop\startProject\OAS.Cloud.PAAS_Interface\common\readCase.py", line 18, in getCase
    self.wb = load_workbook(filename='caseModel.xlsx', read_only=True)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\site-packages\openpyxl\reader\excel.py", line 313, in load_workbook
    data_only, keep_links)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\site-packages\openpyxl\reader\excel.py", line 124, in __init__
    self.archive = _validate_archive(fn)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\site-packages\openpyxl\reader\excel.py", line 96, in _validate_archive
    archive = ZipFile(filename, 'r')
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\zipfile.py", line 1207, in __init__
    self.fp = io.open(file, filemode)
FileNotFoundError: [Errno 2] No such file or directory: 'caseModel.xlsx'

Process finished with exit code 1

The module is taken in a relative path when running in this file, which will find the directory of the current folder. But when he called, calling its files in another directory, perhaps for this reason it can not successfully call. This is my guess, we are now going to verify it.

solution

1, the absolute path validation

f.wb = load_workbook(filename=r'D:\360MoveData\Users\lenovo\Desktop\startProject\OAS.Cloud.PAAS_Interface\common\caseModel.xlsx', read_only=True, )

Validation results:
Here Insert Picture Description
description, our guess is correct. I rely on, I was a fucking genius!

2, os generate absolute path

Code migration to other places, we can not continue to change the path, right? Therefore, we adopted os module to generate an absolute path, so to solve this problem.

        dir_path = os.path.dirname(os.path.abspath(__file__))
        self.wb = load_workbook(filename=os.path.join(dir_path,'caseModel.xlsx'), read_only=True, )

The results show:
Here Insert Picture Description

Published 25 original articles · won praise 0 · Views 2662

Guess you like

Origin blog.csdn.net/weixin_43431593/article/details/104080147