V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Dec 31, 2016 · 2941 views
    This topic created in 3409 days ago, the information mentioned may be changed or developed.

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

    14 replies    2016-12-31 23:00:10 +08:00
    imn1
        1
    imn1  
       Dec 31, 2016
    权限?
    misaka19000
        2
    misaka19000  
       Dec 31, 2016 via Android
    看一下路径?
    bakabie
        3
    bakabie  
    OP
       Dec 31, 2016
    @imn1 同样都是管理员权限,同样用 cmdshell 执行 flas 工程脚本也是无法读取到,不过写文件什么的却依旧可以
    bakabie
        4
    bakabie  
    OP
       Dec 31, 2016
    @misaka19000 路径是正确的,不然读不到["content","data","title"]这个 listdir 的_(:з)∠)_
    manhere
        5
    manhere  
       Dec 31, 2016
    Archiver
    bakabie
        6
    bakabie  
    OP
       Dec 31, 2016
    @manhere 尴尬了。。。不过幸好是别人的代码,不然真的丢脸了
    icedx
        7
    icedx  
       Dec 31, 2016 via Android
    请贴出代码
    bakabie
        8
    bakabie  
    OP
       Dec 31, 2016
    @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  
       Dec 31, 2016
    @bakabie 我感觉是出错了 把调试打开 应该能知道是哪里错了
    debug=1
    weyou
        10
    weyou  
       Dec 31, 2016 via Android
    @bakabie 前面已经 read 过了,文件指针已经到了文件尾部,继续调用 read 当然是空的。手机上没仔细看你代码,说错勿怪。
    weyou
        11
    weyou  
       Dec 31, 2016 via Android
    另外, ruote 又是什么鬼
    Technetiumer
        12
    Technetiumer  
       Dec 31, 2016
    9bie ?
    bakabie
        13
    bakabie  
    OP
       Dec 31, 2016
    @weyou 我也知道 read 文件指针会移动到尾部。。然而第一次读取貌似也是什么都没有。。 route 。。应该是 ruote 。。手打打太快了_(:з)∠)_。。。。
    weyou
        14
    weyou  
       Dec 31, 2016 via Android
    @bakabie 第一次读的文件内容保存到 tempList 里了,你把 tempList 打印出来看看
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2529 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 06:26 · PVG 14:26 · LAX 23:26 · JFK 02:26
    ♥ Do have faith in what you're doing.