简体   繁体   English

如何在python中转义正斜杠,以便open()将我的文件视为要写入的文件名,而不是要读取的文件路径?

[英]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?

Let me preface this by saying I'm not exactly sure what is happening with my code; 让我先说一下,我不确定我的代码发生了什么; I'm fairly new to programming. 我对编程很新。

I've been working on creating an individual final project for my python CS class that checks my teacher's website on a daily basis and determines if he's changed any of the web pages on his website since the last time the program ran or not. 我一直在为我的python CS课程创建一个单独的最终项目,每天检查我的老师的网站,并确定自上次程序运行以来他是否更改了他网站上的任何网页。

The step I'm working on right now is as follows: 我现在正在做的步骤如下:

def write_pages_files():
    '''
    Writes the various page files from the website's links 
    '''
    links = get_site_links()
    for page in links:
        site_page = requests.get(root_url + page)
        soup = BeautifulSoup(site_page.text)
        with open(page + ".txt", mode='wt', encoding='utf-8') as out_file:
            out_file.write(str(soup))

The links look similar to this: 链接看起来类似于:

/site/sitename/class/final-code /网站/网站名称/等级/最终代码

And the error I get is as follows: 我得到的错误如下:

with open(page + ".txt", mode='wt', encoding='utf-8') as out_file:
FileNotFoundError: [Errno 2] No such file or directory: '/site/sitename/class.txt'

How can I write the site pages with these types of names (/site/sitename/nameofpage.txt)? 如何使用这些类型的名称(/site/sitename/nameofpage.txt)编写网站页面?

you cannot have / in the file basename on unix or windows, you could replace / with . 你不能拥有/在unix或windows上的文件basename中,你可以替换/ with . :

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

Python presumes /site etc.. is a directory. Python假设/site等..是一个目录。

On Unix/Mac OS, for the middle slashes, you can use : which will convert to / when viewed, but trigger the subfolders that / does. 在Unix / Mac OS上,对于中间斜杠,您可以使用:将转换为/在查看时,但会触发/执行的子文件夹。

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. site/sitename/class/final-code - > final-code文件位于当前文件夹site:sitename:class:final-codesite文件夹中的sitename文件夹中的class文件夹中site:sitename:class:final-code - > site/sitename/class/final-code当前文件夹中的site/sitename/class/final-code文件。

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'\∕' . 与问题的标题相关,虽然不是具体的,如果你真的希望你的文件名包含一些看起来像斜杠的东西,你可以使用unicode字符“/”( DIVISION SLASH ),也就是u'\∕'

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. 这在大多数情况下都没用(并且可能令人困惑),但是当您希望包含在文件名中的概念的标准命名法包括斜杠时,它可能很有用。

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

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