Python Forum
Unable to get function to do anything...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to get function to do anything...
#1
Hello,

I am a novice at Python, i using Thonny on Raspberry OS with Python 3.9.2
The def in question is listed below( I hope) and to simplify things i have removed it from the main code i am creating an just isolated to make it simpler to Troubleshoot.
the code is below:-

def up_msg_log():
    # Load the workbook and select the sheet & next row
    fpath=os.path.join('/','home','john','NEWRPi4MoistureFiles','msglog.xlsx')
    wb = load_workbook(fpath)
    sheet = wb['Sheet1']
    alignment=Alignment(horizontal='center')
    ins_row = str(len(sheet['A']) + 1 )
    if (drywet)=="Wet":
        row = (cdate,ctime,drywet,"N/A","N/A")
    else:
        row = (cdate,ctime,drywet,opentime1,valveonoff)
    sheet.append(row)
    wb.save(fpath)
# End Def


print('start')
up_msg_log()
print('OK')
The errors i get are :-
>>> %Run deftest.py
Output:
start Traceback (most recent call last): File "/home/john/NEWRPi4MoistureFiles/deftest.py", line 23, in <module> up_msg_log() File "/home/john/NEWRPi4MoistureFiles/deftest.py", line 9, in up_msg_log wb = load_workbook(fpath) File "/home/john/.local/lib/python3.9/site-packages/openpyxl/reader/excel.py", line 344, in load_workbook reader = ExcelReader(filename, read_only, keep_vba, File "/home/john/.local/lib/python3.9/site-packages/openpyxl/reader/excel.py", line 123, in __init__ self.archive = _validate_archive(fn) File "/home/john/.local/lib/python3.9/site-packages/openpyxl/reader/excel.py", line 95, in _validate_archive archive = ZipFile(filename, 'r') File "/usr/lib/python3.9/zipfile.py", line 1257, in __init__ self._RealGetContents() File "/usr/lib/python3.9/zipfile.py", line 1324, in _RealGetContents raise BadZipFile("File is not a zip file") zipfile.BadZipFile: File is not a zip file >>> The first line of code in def is line 8 and it reads: fpath=os.path.join('/','home','john','NEWRPi4MoistureFiles','msglog.xlsx')
This line works OK when used in the main code stream, but i suspect there are issue with it when i move it to the function.

but my limited experience does not give me any insight into the real issue here.

Any help would be appreciated

Thanks

JohnW
deanhystad write Nov-06-2023, 03:10 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Apologies, forgot to ad what is imported


from openpyxl.styles import Alignment
from openpyxl import load_workbook
import os
Reply
#3
deanhystad,

Apologies, and thanks for sorting that out for me.

JohnW
Reply
#4
This part of the original message got mixed up with the errors , so i am using my newfound into to post the code correctly..

The first line of code in def is line 8 (Referenced in the errors) and it reads:
fpath=os.path.join('/','home','john','NEWRPi4MoistureFiles','msglog.xlsx')
and i suspect could be the route cause of the issues, this line works fine when i use it within the main stream of the code but does appear to like being included in the def.

JohnW
Reply
#5
Those Microsoft Office files are zip files.

You get the error:

Quote:zipfile.BadZipFile: File is not a zip file

Something is wrong with your path or your file.

Save a simple XL file and then try to open it:

path2XL = '/home/john/NEWRPi4MoistureFiles/XLfile.xlsx'
wb = load_workbook(path2XL)
Of course I don't have this path or file on my laptop, so if I write:

fpath = '/home/john/NEWRPi4MoistureFiles/msglog.xlsx'
os.path.isfile(fpath)
I get
Output:
False
If the path was good, openpyxl would open it, no trouble.
So build a file check into your function!
Reply
#6
Hello,

Thank you for your response.

My working environment for this work is on Raspberry OS, so there is no Microsoft office, the .xlsx file was created using libre Office but with xlsx extension on output, Would that cause such issues?

ref your comment on build a File check, could i ask you to expand on that please?

Thank You
JohnW
Reply
#7
Hmmmm...

OK based on your response indicating that the path was probably the issue. i deleted the existing .xlsx files and used libre Office to create new files... and now the def works so not sure what went on there.

However i am interested in the suggestion made to build a check file into the def, so anything on that would be very usefull thank you.

JohnW
Reply
#8
Well, I'm no expert, but I often need to do the same thing to various XL files, so I make a list of the files, then choose 1 from the list. Then I either loop, or offer myself the choice of file again.

Something like this:

import os
import glob

mypath = '/home/pedro/myPython/openpyxl/xlsx_files/'
# can add files from various paths to xlfiles if needed
xlfiles = glob.glob(mypath + '*.xlsx')
for x in xlfiles:
    print('The XL files are', x)

myfile = input('Copy and paste the whole path to the file you want from above ... ')

while not os.path.isfile(myfile):
    print('Something wrong with this path or file. Choose another file.')
    myfile = input('Copy and paste the whole path to the file you want from above ... ')
If myfile is not a valid file, os.path.isfile(myfile) returns False. However, this does not check if it is a valid XL file, just a valid file. Could be any type of file.

Maybe check if it is a zip file and ends in .xlsx Not sure how to check if it is a zip file! Big Grin Big Grin Big Grin

But the experts here will know how to do that! Have fun!
Reply
#9
(Nov-07-2023, 07:38 AM)Pedroski55 Wrote: Well, I'm no expert, but I often need to do the same thing to various XL files, so I make a list of the files, then choose 1 from the list. Then I either loop, or offer myself the choice of file again.

Something like this:

import os
import glob

mypath = '/home/pedro/myPython/openpyxl/xlsx_files/'
# can add files from various paths to xlfiles if needed
xlfiles = glob.glob(mypath + '*.xlsx')
for x in xlfiles:
    print('The XL files are', x)

myfile = input('Copy and paste the whole path to the file you want from above ... ')

while not os.path.isfile(myfile):
    print('Something wrong with this path or file. Choose another file.')
    myfile = input('Copy and paste the whole path to the file you want from above ... ')
If myfile is not a valid file, os.path.isfile(myfile) returns False. However, this does not check if it is a valid XL file, just a valid file. Could be any type of file.

Maybe check if it is a zip file and ends in .xlsx Not sure how to check if it is a zip file! Big Grin Big Grin Big Grin

But the experts here will know how to do that! Have fun!
Hello,

Thank You for this, OK i understand, when i read the suggestion first i thought it was about creating an Exception Error... my mistake.

Again thanks

Johnw
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to use result of solver in another function ross1993hall 0 1,421 Aug-10-2020, 10:29 AM
Last Post: ross1993hall
  Passing Values of Dictionaries to Function & Unable to Access Them firebird 3 2,597 Aug-03-2019, 10:25 PM
Last Post: firebird

Forum Jump:

User Panel Messages

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