0

I am trying to read in a list of Excel files from a share drive in Windows using pandas.

For example,

import pandas as pd
df = pd.DataFrame()

# new_files is a list of file paths to the Excel files on the share drive
for f in new_files:
    tmp = pd.read_excel(f)
    df = pd.concat([df, tmp], axis=0)

The file names in new_files are generated by an automated process and have very long file paths (more than 259 characters).

When I try to read the files I get the below error.

Pandas Error: FileNotFoundError: [Err no 2] No such file or directory

At first I thought my share drive parent path was wrong, but it is fine,

import os
path_ = '\\\\acme.corp\\path\\to\\lots\\more\\sub\\folders'
os.path.exists(path_)
True

But the full file path does not exist (although I can see the file in the terminal),

os.path.exists(os.path.join(path_, 'very long filename.xlsx'))
False

When I opened it in Excel it seems this is actually a Windows/Excel problem,

Microsoft Excel Error: Cannot open the file because the file path is more than 259 characters. Try shortening the path or file name

If I copy the file to a local drive or shorter path then I can open it with pandas and Excel which means the files are fine.

I was wondering if there is a way to read these Excel files in pandas (or Python) with these very long file paths?

To avoid copying the files to another location as they are large.

I am running on Windows 10, Python 3.9.5, Pandas 1.3.1

RK1
  • 2,384
  • 1
  • 19
  • 36
  • 1
    I believe it's a Windows/OS problem and I don't think Pandas/Python can go around it. It looks like you are working with a folder on a network drive, can you mount that folder to Windows, e.g. `Z:`. – Quang Hoang Dec 14 '21 at 20:14
  • Yes seems so! I tried using `shutil.copy` to move the files to a shorter directory but get the same issue, think the mount is a good shout, looking [here](https://stackoverflow.com/questions/1271317/what-is-the-best-way-to-map-windows-drives-using-python) – RK1 Dec 14 '21 at 20:18

0 Answers0