0

I've been writing some code that takes a series of Excel files as an input, and then saves a dataframe to an excel as an output. Both the input and output folders sit on a network drive, where

InputFolder = r"\\networklocation\\folder1\\folder2\\input\\"
OutputFolder = r"\\networklocation\\folder1\\folder2\\output\\"

The code is able to read the input files correctly with pd.read_excel(InputFolder +"\\filename"), but when saving the output with df.to_excel(OutputFolder +"\\filename") an error is thrown of "OSError: Cannot save file into a non-existent directory", where the output folder does exist.

Strangely as well this error does not exist on all devices, when running on other PCs with the exact same access, some are able to save to the output folder with no issue.

I've tried changing the folder location string to use raw string literals, but this had no effect. I've also tried changing the output code to save the excel to the InputFolder, but it still threw the non-existent directory error, even though it had just read a file from the same location.

2
  • 1
    You seem to be mixing paradigms here. A network drive directory would look like r"\\networklocation\folder1\folder2\input" (which can't end with a backslash) or "\\\\networklocation\\folder1\\folder2\\input\\" but you have too many double slashes, or too few in the beginning if you take out the r sigil.
    – tripleee
    Commented Jul 9 at 9:35
  • Does your problem only occur with the excel file? Can you reproduce it without pandas?
    – ken
    Commented Jul 9 at 9:43

1 Answer 1

1

If your output directory path is defined as follows:

OutputFolder = r"\\networklocation\\folder1\\folder2\\output\\"

Then you need to save the the file as follows:

df.to_excel(OutputFolder +"filename.xlsx")

Note: Confirm that slashes are not getting dub-bled up while concatenating directory path and file name.

However, if the directory path is defined as follows:

OutputFolder = r"\\networklocation\\folder1\\folder2\\output"

then, save the file as follows:

df.to_excel(OutputFolder +"\\filename.xlsx")

Not the answer you're looking for? Browse other questions tagged or ask your own question.