Reputation: 323
I want to save some plotly graph object figures as a svg file. My code looks like this:
import plotly.io as pio
pio.write_image(figOIA, "S:\FC\FCD\06_Datenbanken\FCD Dashboard\Plots Budget-Ausschöpfung/asdf.svg"))
But I get an error saying this:
> OSError: [Errno 22] Invalid argument: 'S:\\FC\\FCD\x06_Datenbanken\\FCD Dashboard\\Plots
> Budget-Ausschöpfung/asdf.svg'
Apparently, the write_image() function changed my directory. Why is it adding the "/" and the "x" in front of "06_Datenbanken" ? It's so frustrating as I have no clue how that can happen, any help is highly aprreciated, thank you!
From the answers I know now what the mistake was. But when I now try this code:
raw_string = r"S:\FC\FCD\06_Datenbanken\FCD Dashboard\Plot Budget-Ausschöpfung" + r"/" + r"{}".format(today.year) + r"_" + r"{}".format(today.month) + r"_" + r"{}".format(Ressort_value) + r"_Budget-Ausschöpfung.svg"
pio.write_image(figOIA, raw_string )
I still get this error:
> FileNotFoundError: [Errno 2] No such file or directory: 'S:\\FC\\FCD\\06_Datenbanken\\FCD Dashboard\\Plot
> Budget-Ausschöpfung/2020_7_Alle_Budget-Ausschöpfung.svg'
So the double backslashes are still there.. what is wrong this time?
Upvotes: 0
Views: 1758
Reputation: 308520
The backslash character \
is used as a special escape character in Python strings. In order to defeat this special processing you must either double them up as \\
or use a raw string.
pio.write_image(figOIA, "S:\\FC\\FCD\\06_Datenbanken\\FCD Dashboard\\Plots Budget-Ausschöpfung/asdf.svg"))
or
pio.write_image(figOIA, r"S:\FC\FCD\06_Datenbanken\FCD Dashboard\Plots Budget-Ausschöpfung/asdf.svg"))
Upvotes: 1