python - When converting path variable in a list it prints extra symbols \\
问题描述
First in my code I created variable rootdir:
rootdir = r'\\001\Server\Folder1\Folder2\Folder3\Folder4' # shared drive
print(rootdir)
Output:
\\001\Server\Folder1\Folder2\Folder3\Folder4 # looks good
Then I'm creating a list of paths, so I can attach files from that location
files = [rootdir + '\\' + 'MyFile.xlsx']
print(files)
Output:
['\\\\001\\Server\\Folder1\\Folder2\\Folder3\\Folder4\\MyFile.xlsx'] # has extra \\
I need to use list of paths in a function:
# 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()
Now the path has extra \\
Why does it happen and how can I get rid of those \\
to get path look like this:
\\001\Server\Folder1\Folder2\Folder3\Folder4\MyFile.xlsx
解决方案
That's do do with how printing a list evaluates. The actual contents of the list is as you want.
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
推荐阅读
- node.js - 检测 Whatsapp 图片上传源
- node.js - 如何使用 nodejs 将图像上传到 s3 存储桶?
- python - 更新其他列匹配条件的列未按预期工作
- python - 如何设置熊猫数据框中某个字段的值?
- angular - ASP.NET Core 中的 Angular 应用 6:多个模块匹配
- android - kotlin 正则表达式中无法识别的反斜杠转义
- node.js - 如何在节点js中通过url读取文件
- linux - 静默安装后侦听器未启动
- linux - ICMP 回显响应数据包在内核和用户空间之间消失
- java - 使用 Spring-boot 2 在 postGis 中存储 long/lat JSON 的最佳数据类型