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-07-16 22:49:07 +08:00 · 3433 次点击
    这是一个创建于 4307 天前的主题,其中的信息可能已经有所发展或是发生改变。
    程序里需要获取指定目录的文件列表和文件的一些信息,写了个getflist():

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

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

    另,最后一行多了一个括号。
    luztak
        2
    luztak  
    OP
       2012-07-16 23:45:38 +08:00
    @reorx 啊,手打木注意>_<
    问题是fl和path是基准变量,后续还有变量要用它们,所以是这么写的.
    也尝试过getflist()返回值,用全局变量接收,但在类函数里直接Error.
    getflist里global var也试过,但没有效果.
    我倒是想过重启……但环境是不允许重启的……不然我就这么写了>_<
    reorx
        3
    reorx  
       2012-07-17 01:30:52 +08:00   ❤️ 1
    luztak
        4
    luztak  
    OP
       2012-07-17 05:55:45 +08:00
    @reorx 把Gist浏览器地址的https换成http……
    luztak
        5
    luztak  
    OP
       2012-07-17 05:56:11 +08:00
    luztak
        6
    luztak  
    OP
       2012-07-17 05:57:38 +08:00
    @reorx 这样啊……我试试.Thx~
    keakon
        7
    keakon  
       2012-07-17 17:00:10 +08:00
    @livid 这个 gist 的 js 和 css 载入严重拖慢了网页加载,建议放在最下面处理。
    whtsky
        8
    whtsky  
       2012-07-17 18:02:04 +08:00
    @keakon 请教一下,怎样才能延迟载入gist的js呢?官方提供的js是用document.write实现插入的..
    cute
        9
    cute  
       2012-07-17 18:03:21 +08:00   ❤️ 1
    可以参考dircache模块
    luztak
        10
    luztak  
    OP
       2012-07-17 18:52:25 +08:00
    @reorx cute 谢谢哦,已经解决了.一个time.sleep()就完事了- -||
    keakon
        11
    keakon  
       2012-07-17 20:04:07 +08:00   ❤️ 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-07-17 20:08:35 +08:00
    @keakon 应该HTML实体转义了吧这里......也许@Livid 可以写一个自定义函数处理gist.
    whtsky
        13
    whtsky  
       2012-07-17 20:18:18 +08:00   ❤️ 1
    @keakon it works.好方法。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2290 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:34 · PVG 16:34 · LAX 01:34 · JFK 04:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.