V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
luztak
V2EX  ›  Python

Python刷新文件列表

  •  
  •   luztak ·
    luztak · 2012 年 7 月 16 日 · 3845 次点击
    这是一个创建于 4933 天前的主题,其中的信息可能已经有所发展或是发生改变。
    程序里需要获取指定目录的文件列表和文件的一些信息,写了个getflist():

    在之后的函数里确实能使用fl,内容也是正确的.之后有些创建文件的操作,在类函数里执行getflist(),但之后读取的fl并没有更新.请问这是为什么?
    13 条回复    1970-01-01 08:00:00 +08:00
    reorx
        1
    reorx  
       2012 年 7 月 16 日
    可能是由于`fl`被定义为全局变量,如果你只是删除文件,`fl`就不会变。

    把`fl`和`path`两个变量放在函数里吧,这样规范些。

    另,最后一行多了一个括号。
    luztak
        2
    luztak  
    OP
       2012 年 7 月 16 日
    @reorx 啊,手打木注意>_<
    问题是fl和path是基准变量,后续还有变量要用它们,所以是这么写的.
    也尝试过getflist()返回值,用全局变量接收,但在类函数里直接Error.
    getflist里global var也试过,但没有效果.
    我倒是想过重启……但环境是不允许重启的……不然我就这么写了>_<
    reorx
        3
    reorx  
       2012 年 7 月 17 日   ❤️ 1
    luztak
        4
    luztak  
    OP
       2012 年 7 月 17 日
    @reorx 把Gist浏览器地址的https换成http……
    luztak
        5
    luztak  
    OP
       2012 年 7 月 17 日
    luztak
        6
    luztak  
    OP
       2012 年 7 月 17 日
    @reorx 这样啊……我试试.Thx~
    keakon
        7
    keakon  
       2012 年 7 月 17 日
    @livid 这个 gist 的 js 和 css 载入严重拖慢了网页加载,建议放在最下面处理。
    whtsky
        8
    whtsky  
       2012 年 7 月 17 日
    @keakon 请教一下,怎样才能延迟载入gist的js呢?官方提供的js是用document.write实现插入的..
    cute
        9
    cute  
       2012 年 7 月 17 日   ❤️ 1
    可以参考dircache模块
    luztak
        10
    luztak  
    OP
       2012 年 7 月 17 日
    @reorx cute 谢谢哦,已经解决了.一个time.sleep()就完事了- -||
    keakon
        11
    keakon  
       2012 年 7 月 17 日   ❤️ 1
    @whtsky 好吧,那就只能很ugly地实现了,如果github不更改id的命名规则的话…
    此外,script放div里好像是不符合标准的。


    Python刷新文件列表
    <div id="gist-3123099-container"></div>
    在之后的函数里确实能使用fl,内容也是正确的.之后有些创建文件的操作,在类函数里执行getflist(),但之后读取的fl并没有更新.请问这是为什么?
    <div style="display:none">
    <script>
    function fill_container(gist_id) {
    document.getElementById('gist-' + gist_id + '-container').appendChild(document.getElementById('gist-' + gist_id));
    }
    </script>
    <script src="https://gist.github.com/3123099.js?file=getflist.py" onload="fill_container(3123099)"></script>
    </div>
    luztak
        12
    luztak  
    OP
       2012 年 7 月 17 日
    @keakon 应该HTML实体转义了吧这里......也许@Livid 可以写一个自定义函数处理gist.
    whtsky
        13
    whtsky  
       2012 年 7 月 17 日   ❤️ 1
    @keakon it works.好方法。
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   2753 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 07:23 · PVG 15:23 · LAX 23:23 · JFK 02:23
    ♥ Do have faith in what you're doing.