简体   繁体   中英

plotly.io.write_image() changes my given filepath by adding “/” which makes the path useless

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?

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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM