代码之家  ›  专栏  ›  技术社区  ›  Nihal Saranga

文本文件名包含/,并在目录[duplicate]中创建它

  •  0
  • Nihal Saranga  · 技术社区  · 6 年前

    不太确定 我的代码发生了什么;我对编程还相当陌生。

    我现在正在进行的步骤如下:

    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))
    

    /站点/站点名称/类/最终代码

    我得到的误差如下:

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

    如何使用这些类型的名称(/site/sitename)编写站点页面/页面名称.txt)?

    0 回复  |  直到 9 年前
        1
  •  10
  •   Padraic Cunningham    9 年前

    你不能有 / 在unix或windows上的basename文件中,可以替换 具有 . :

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

    蟒蛇假设 /site

        2
  •  4
  •   Fleija    9 年前

    在Unix/Mac OS上,对于中间斜杠,可以使用 : 将转换为 / / 做。

    site/sitename/class/final-code final-code 文件 class sitename 文件夹 site 当前文件夹中的文件夹 site:sitename:class:final-code 当前文件夹中的文件。

        3
  •  4
  •   Lucas Wiman    7 年前

    与问题的标题有关,但不是细节,如果你 要使文件名包含类似斜杠的内容,可以使用unicode字符“”( DIVISION SLASH ),又名 u'\u2215' .

    这在大多数情况下都没有用(而且可能会令人困惑),但当您希望包含在文件名中的概念的标准命名法包含斜杠时,这可能会很有用。