请教一个文件读取的问题

2016-12-31 10:06:09 +08:00
 bakabie

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

2458 次点击
所在节点    Python
14 条回复
imn1
2016-12-31 10:09:43 +08:00
权限?
misaka19000
2016-12-31 10:11:27 +08:00
看一下路径?
bakabie
2016-12-31 10:11:39 +08:00
@imn1 同样都是管理员权限,同样用 cmdshell 执行 flas 工程脚本也是无法读取到,不过写文件什么的却依旧可以
bakabie
2016-12-31 10:12:31 +08:00
@misaka19000 路径是正确的,不然读不到["content","data","title"]这个 listdir 的_(:з)∠)_
manhere
2016-12-31 10:13:47 +08:00
Archiver
bakabie
2016-12-31 10:15:10 +08:00
@manhere 尴尬了。。。不过幸好是别人的代码,不然真的丢脸了
icedx
2016-12-31 10:26:34 +08:00
请贴出代码
bakabie
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
2016-12-31 14:13:57 +08:00
@bakabie 我感觉是出错了 把调试打开 应该能知道是哪里错了
debug=1
weyou
2016-12-31 14:36:15 +08:00
@bakabie 前面已经 read 过了,文件指针已经到了文件尾部,继续调用 read 当然是空的。手机上没仔细看你代码,说错勿怪。
weyou
2016-12-31 14:36:56 +08:00
另外, ruote 又是什么鬼
Technetiumer
2016-12-31 15:25:16 +08:00
9bie ?
bakabie
2016-12-31 17:53:25 +08:00
@weyou 我也知道 read 文件指针会移动到尾部。。然而第一次读取貌似也是什么都没有。。 route 。。应该是 ruote 。。手打打太快了_(:з)∠)_。。。。
weyou
2016-12-31 23:00:10 +08:00
@bakabie 第一次读的文件内容保存到 tempList 里了,你把 tempList 打印出来看看

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/331397

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX