[英]Saving file with a name containing / at the destination folder in python
I am saving emails from outlook to the local folder.Folder path is following.我正在将来自 outlook 的电子邮件保存到本地文件夹。文件夹路径如下。
folder_path=r"C:\Documents\emails
filename is the subject of the email message as the following文件名是 email 消息的主题,如下所示
subject=email_message.subject
so the final_path will be所以 final_path 将是
final_path=os.path.join(folder_path,subject+".eml")
sometimes the subject contains "/" and then it gives the following error有时主题包含“/”然后它给出以下错误
[Error2] No such file or directory: 'C:\\Documents\\emails\\test1/email_123'
I think this is because that extra "/" in the subject line (subject was "test1/email_123")我认为这是因为主题行中的额外“/”(主题是“test1/email_123”)
How can I fix this?我怎样才能解决这个问题?
This depends on how you want to treat the /
in the subject line.这取决于您希望如何处理主题行中的
/
。 Use the .replace()
function on the subject accordingly.相应地在主题上使用
.replace()
function。
Ignore /
忽略
/
subject=email_message.subject.replace("/", "")
Then the directory would be: 'C:\Documents\emails\test1email_123'那么目录将是:'C:\Documents\emails\test1email_123'
Treat /
as a directory structure将
/
视为目录结构
subject=email_message.subject.replace("/", "\")
Then the directory would be: 'C:\Documents\emails\test1\email_123'那么目录将是:'C:\Documents\emails\test1\email_123'
Treat /
as a special character将
/
视为特殊字符
If a /
means something else in your organization like a hyphen or an underscore then use it.如果
/
表示您组织中的其他内容,例如连字符或下划线,请使用它。
subject=email_message.subject.replace("/", "-")
Then the directory would be: 'C:\Documents\emails\test1-email_123'那么目录将是:'C:\Documents\emails\test1-email_123'
you should replace the letter with something else.你应该用别的东西代替这封信。
like 'C:\Documents\emails\test1/email_123' should be converted into 'C:\Documents\emails\test1_email_123'像 'C:\Documents\emails\test1/email_123' 应该转换成 'C:\Documents\emails\test1_email_123'
this can be done by putting one line before final_path=os.path.join(folder_path,subject+".eml")
这可以通过在
final_path=os.path.join(folder_path,subject+".eml")
之前放置一行来完成
subject.replace('/','_');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.