简体   繁体   English

如何在Python中跳过**正斜杠**字符

[英]How to skip **forward slash** char in python

Can anyone tell how to skip forward slash char in python? 谁能说出如何在python中跳过正斜杠char?

I want to create a directory abc(17/12/18) so I tried 我想创建一个目录abc(17/12/18),所以我尝试了

import os
os.makedirs('abc(17\/12\/18)')

but the folder created was abc(17\\) 但是创建的文件夹是abc(17 \\)

Can anyone tell what am I missing? 谁能说出我在想什么? I searched on Internet but was unsuccessful. 我在Internet上搜索,但未成功。

You don't need to escape forward slashes in python, only backslashes. 您无需在python中转义正斜杠,只需反斜杠即可。 The reason you cannot use that filename is that forward slashes are illegal in windows filenames. 无法使用该文件名的原因是Windows文件名中的正斜杠是非法的。 try this: 尝试这个:

import os
os.makedirs('abc(17-12-18)')

You could do this. 你可以做到这一点。

import os

os.makedirs('abc(17' + u'\u2215' + '12' + u'\u2215' + '18)')

# This will create a directory named abc(17∕12∕18)

In Windows and Linux, / is not allowed in folder name. 在Windows和Linux中,文件夹名称中不允许/

More information here: https://stackoverflow.com/a/31976060/3813027 此处了解更多信息: https : //stackoverflow.com/a/31976060/3813027

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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