How do I escape forward slashes in python, so that open() sees my file as a filename to write, instead of a filepath to read?

三世轮回 提交于 2019-12-03 12:18:05

you cannot have / in the file basename on unix or windows, you could replace / with .:

page.replace("/",".") + ".txt"

Python presumes /site etc.. is a directory.

On Unix/Mac OS, for the middle slashes, you can use : which will convert to / when viewed, but trigger the subfolders that / does.

site/sitename/class/final-code -> final-code file in a class folder in a sitename folder in a site folder in the current folder site:sitename:class:final-code -> site/sitename/class/final-code file in the current folder.

Related to the title of the question, though not the specifics, if you really want your file names to include something that looks like a slash, you can use the unicode character "∕" (DIVISION SLASH), aka u'\u2215'.

This isn't useful in most circumstances (and could be confusing), but can be useful when the standard nomenclature for a concept you wish to include in a filename includes slashes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!