Python on Linux says 'no such file or directory' even if it exists

Issue

I’m getting the error no such file or directory from Python when I try to open a .xlsx file with openpyxl.

It’s strange because:
All files (.py and .xlsx) are stored on my NAS.
The script reads several .xlsx files from different directories.
If I’m running the script on macOS (via SMB), everything works fine.
Python is in the correct directory and the file is definitely there. The name is 100% identical.
I can open the files manually, so they’re not damaged.

But on my Linux VM running on my Server (via mount), the error occurs, but only at the 3rd file (the first two can be read).
They just differ in the fact that they were saved from gmail, but however, they don’t look damaged etc.
The code for saving them:

mail = imaplib.IMAP4_SSL("imap.gmail.com" )
mail.login("[email protected]","password")
mail.select('INBOX')
data = mail.search(None, 'ALL')
mail_ids = data[1]
id_list = mail_ids[0].split()
if len(id_list)>=1:
    first_email_id = int(id_list[0])
    latest_email_id = int(id_list[-1])

    for i in range(latest_email_id,first_email_id-1, -1):
        data = mail.fetch(str(i), '(RFC822)' )
        for response_part in data:
            arr = response_part[0]
            if isinstance(arr, tuple):
                msg = email.message_from_string(str(arr[1],'utf-8'))
                email_subject = msg['subject']
                email_to = msg['to']

                counter = 1
                for part in msg.walk():
                    # multipart/* are just containers
                    if part.get_content_maintype() == 'multipart':
                        continue
                    # Applications should really sanitize the given filename so that an
                    # email message can't be used to overwrite important files
                    filename = part.get_filename()
                    if not filename:
                        ext = mimetypes.guess_extension(part.get_content_type())
                        if not ext:
                            # Use a generic bag-of-bits extension
                            ext = '.bin'
                        filename = f'part-{counter:03d}{ext}'
                    counter += 1
                    if ".xls" in filename:
                        with open(os.path.join(".", filename), 'wb') as fp:
                            fp.write(part.get_payload(decode=True))

The error occurs here:

if xyz == 1:
    Excel = "Gleichmäßigkeit_A+R.xlsx"
...
os.chdir("Klassen")
TKlasse1 = lw(Excel) # this file cant be opened, "lw" for openpyxl.load_workbook
TKlasse1 = TKlasse1["WL1"]

The full error output is:

[email protected]:/mnt/paul/server/dhm$ python3 DHM.py
Traceback (most recent call last):
  File "DHM.py", line 347, in <module>
    TKlasse1 = lw(Excel)
  File "/usr/local/lib/python3.8/dist-packages/openpyxl/reader/excel.py", line 315, in load_workbook
    reader = ExcelReader(filename, read_only, keep_vba,
  File "/usr/local/lib/python3.8/dist-packages/openpyxl/reader/excel.py", line 124, in __init__
    self.archive = _validate_archive(fn)
  File "/usr/local/lib/python3.8/dist-packages/openpyxl/reader/excel.py", line 96, in _validate_archive
    archive = ZipFile(filename, 'r')
  File "/usr/lib/python3.8/zipfile.py", line 1251, in __init__
    self.fp = io.open(file, filemode)
FileNotFoundError: [Errno 2] No such file or directory: 'Gleichmäßigkeit_A+R.xlsx'

Solution

The name wasn’t identical. It looked that way, but the name contained an "ä" which wasn’t recognized correctly by Linux.

Answered By – server_paul

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published