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
bakabie
V2EX  ›  Python

请教一个文件读取的问题

  •  
  •   bakabie ·
    9bie · 2016-12-31 10:06:09 +08:00 · 2439 次点击
    这是一个创建于 2644 天前的主题,其中的信息可能已经有所发展或是发生改变。

    虽然我知道在 windows 下写 python 并不友好,但是因为实际需要。。 同样一个目录,一个是 flask 工程文件,一个是 cmdshell 下直接运行的 python 文件,用同样的方法读取文件,为何 flask 工程文件读取不到呢 flask 工程文件代码 and 截图: cmdshell 直接运行的截图:

    14 条回复    2016-12-31 23:00:10 +08:00
    imn1
        1
    imn1  
       2016-12-31 10:09:43 +08:00
    权限?
    misaka19000
        2
    misaka19000  
       2016-12-31 10:11:27 +08:00 via Android
    看一下路径?
    bakabie
        3
    bakabie  
    OP
       2016-12-31 10:11:39 +08:00
    @imn1 同样都是管理员权限,同样用 cmdshell 执行 flas 工程脚本也是无法读取到,不过写文件什么的却依旧可以
    bakabie
        4
    bakabie  
    OP
       2016-12-31 10:12:31 +08:00
    @misaka19000 路径是正确的,不然读不到["content","data","title"]这个 listdir 的_(:з)∠)_
    manhere
        5
    manhere  
       2016-12-31 10:13:47 +08:00
    Archiver
    bakabie
        6
    bakabie  
    OP
       2016-12-31 10:15:10 +08:00
    @manhere 尴尬了。。。不过幸好是别人的代码,不然真的丢脸了
    icedx
        7
    icedx  
       2016-12-31 10:26:34 +08:00 via Android
    请贴出代码
    bakabie
        8
    bakabie  
    OP
       2016-12-31 10:32:17 +08:00
    @icedx
    flask 工程代码:

    import flask,os,time
    import shutil
    app = flask.Flask(__name__)
    PageListbuff =[]
    def ArichverData(id):
    pass
    def LoadArichversList():
    tempList = []
    files = os.listdir("Arichvers")
    print files
    #print files
    for item in files:
    titlefile = open("Arichvers/"+str(item)+"/title",'r')
    tempList.append({"href":WebSiteUrl+titlefile.read(),"title":titlefile.read(),"Data":ArichverData(str(item))[0]})
    print os.path.isfile("Arichvers/"+str(item)+"/title")
    print os.listdir("Arichvers/"+str(item))
    print titlefile.read()
    titlefile.close()

    PageListbuff=tempList
    @app.ruote("/")
    def index():
    pass
    if __name__ == '__main__':
    LoadArichversList()

    app.run(host='0.0.0.0',port=80,threaded=True)

    cmdshell 下直接进入 python 执行的代码就在图中,非常短
    icedx
        9
    icedx  
       2016-12-31 14:13:57 +08:00
    @bakabie 我感觉是出错了 把调试打开 应该能知道是哪里错了
    debug=1
    weyou
        10
    weyou  
       2016-12-31 14:36:15 +08:00 via Android
    @bakabie 前面已经 read 过了,文件指针已经到了文件尾部,继续调用 read 当然是空的。手机上没仔细看你代码,说错勿怪。
    weyou
        11
    weyou  
       2016-12-31 14:36:56 +08:00 via Android
    另外, ruote 又是什么鬼
    Technetiumer
        12
    Technetiumer  
       2016-12-31 15:25:16 +08:00
    9bie ?
    bakabie
        13
    bakabie  
    OP
       2016-12-31 17:53:25 +08:00
    @weyou 我也知道 read 文件指针会移动到尾部。。然而第一次读取貌似也是什么都没有。。 route 。。应该是 ruote 。。手打打太快了_(:з)∠)_。。。。
    weyou
        14
    weyou  
       2016-12-31 23:00:10 +08:00 via Android
    @bakabie 第一次读的文件内容保存到 tempList 里了,你把 tempList 打印出来看看
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1554 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 17:14 · PVG 01:14 · LAX 10:14 · JFK 13:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.