"No such file or directory" 使用指向服务器的 ExcelWriter 时

"No such file or directory" when using ExcelWriter pointing to a server

使用 Visual Studio 代码。

Python 3 安装在本地。

存储在服务器上的工作文件。

我正在使用 pandas ExcelWriter 修改 excel 文件。

如果ExcelWriter指向本地路径那么它工作正常,

writer = pd.ExcelWriter('C:\Users\username\Documents\TestingFolder\ExpirationList.xlsx') 

但是如果我将它指向一个服务器位置然后它给我一个错误:

FileNotFoundError: [Errno 2] No such file or directory: '\01.02.03.04\username\Documents\Documents\Python\"Expiration List report email send out"\ExpirationList.xlsx' 

示例代码:

 import pandas as pd 
    import pyodbc 
    import smtplib 
    from email.mime.multipart import MIMEMultipart 
    from email.mime.text import MIMEText 
    from email.mime.base import MIMEBase 
    from email import encoders 
    import datetime
    import calendar


    query = """select top 5 PolicyNumber from tblQuotes where PolicyNumber is not null"""
    conn = pyodbc.connect('DRIVER={SQL Server};server=servername;DATABASE=dbName;Trusted_Connection=yes;')
    df = pd.read_sql_query(query, conn)

    writer = pd.ExcelWriter('C:\Users\username\Documents\TestingFolder\ExpirationList.xlsx') 

    #Does not work
    writer = pd.ExcelWriter('\01.02.03.04\username\Documents\Documents\Python\"Expiration List report email send out"\ExpirationList.xlsx')  

    df.to_excel(writer, sheet_name='Sheet1', startrow=4, header=False, index=False)
    writer.save()

如何使用服务器位置?

Network/server 路径前面需要四个反斜杠。
例如。 \\01.02.03.04\...

如其他答案所示,您可以将反斜杠加倍以获得正确的转义。或者,使用 "raw" 字符串:

r'\01.02.03.04\username\Documents\Documents\Python\"Expiration List report email send out"\ExpirationList.xlsx')  

请注意,none 个反斜杠在此原始字符串中被转义。