在列表中转换路径变量时,它会打印额外的符号 \\

When converting path variable in a list it prints extra symbols \\

首先在我的代码中我创建了变量 rootdir:

rootdir = r'\001\Server\Folder1\Folder2\Folder3\Folder4' # shared drive
print(rootdir)

输出:

\001\Server\Folder1\Folder2\Folder3\Folder4  # looks good

然后我正在创建一个路径列表,这样我就可以从该位置附加文件

files = [rootdir +  '\' + 'MyFile.xlsx']
print(files)

输出:

['\\001\Server\Folder1\Folder2\Folder3\Folder4\MyFile.xlsx'] # has extra \

我需要在函数中使用路径列表:

# function sends email
def send_mail(send_from,rcpt,subject,text ,files):
    assert isinstance(send_to, list)
    assert isinstance(cc, list)
    assert isinstance(files, list) # files need to be a list
    msg = MIMEMultipart()

现在路径有额外的\

为什么会发生这种情况,我怎样才能摆脱那些 \ 以获得如下所示的路径:

\001\Server\Folder1\Folder2\Folder3\Folder4\MyFile.xlsx

这与打印列表的评估方式有关。列表的实际内容如您所愿。

In [9]: print(files)
['\\001\Server\Folder1\Folder2\Folder3\Folder4\MyFile.xlsx']

In [10]: for file in files:
    ...:     print(file)
    ...:
\001\Server\Folder1\Folder2\Folder3\Folder4\MyFile.xlsx