小编Rob*_*ear的帖子

python存储带有正向和反向斜杠的路径名

我有一个过程,os.walk目录及其子目录过滤pdf文件,分离出它们的名称和相应的路径名.我遇到的问题是,它将扫描最顶层的目录并打印相应的文件名,G:/Books/Title.Pdf但第二个它扫描一个子文件夹,例如G:/Books/Sub Folder/Title.pdf它将打印以下内容

G:/Books/Sub Folder\\Title.Pdf
Run Code Online (Sandbox Code Playgroud)

(显然是无效的路径名).它还会将\\添加到子文件夹中的任何子文件夹.

以下是程序:

def dicitonary_list():
    indexlist=[]        #holds all files in the given directory including subfolders
    pdf_filenames=[]    #holds list of all pdf filenames in indexlist
    pdf_dir_list = []   #holds path names to indvidual pdf files 

    for root, dirs,files in os.walk('G:/Books/'):
        for name in files:
            indexlist.append(root + name)
            if ".pdf" in name[-5:]:
                pdf_filenames.append(name)

    for files in indexlist:
        if ".pdf" in files[-5:]:
            pdf_dir_list.append(files)

    dictionary=dict(zip(pdf_filenames, pdf_dir_list))       #maps the pdf names to their directory address
Run Code Online (Sandbox Code Playgroud)

我知道这是一件很简单的事情,我很遗憾,但是对于爱情和金钱我都能看到它是什么.一双新鲜的眼睛会有很大的帮助!

python list os.walk python-2.7

2
推荐指数
1
解决办法
7346
查看次数

瓶python渲染变量作为文本而不是html

我有瓶python的问题,我有以下代码

import glob
import os
from bottle import run, route, error, template
from Find_Posts import hyperlink_postnames

currentdir = os.getcwd()

def hyperlink_postnames():
    hyperlink_filelist = []
    os.chdir(currentdir + "\\Blog_Posts\\")

    for files in glob.glob("*.txt"):
        hyperlink_filelist.append('<a href = "/blog/' + files + '"' + '>' + str(os.path.splitext(files)[0]) + '</a>')
    return  hyperlink_filelist
Run Code Online (Sandbox Code Playgroud)

返回以下列表

['<a href = "/blog/post1.txt">post1</a>', '<a href = "/blog/post2.txt">post2</a>', '<a href = "/blog/post3.txt">post3</a>', '<a href = "/blog/post4.txt">post4</a>', '<a href = "/blog/post5.txt">post5</a>', '<a href = "/blog/post6.txt">post6</a>']
Run Code Online (Sandbox Code Playgroud)

然后进入以下瓶装路线:

@route('/blog/')
def postnames():
    postlist = hyperlink_postnames()
    tpl_out …
Run Code Online (Sandbox Code Playgroud)

python bottle

1
推荐指数
1
解决办法
3092
查看次数

标签 统计

python ×2

bottle ×1

list ×1

os.walk ×1

python-2.7 ×1