python2读写文件错误_尝试在Python中读取文本文件,得到IOError(2,'No this file or directory')...

我按照这里找到的代码,把它放到PyCharm中的一个项目中,但是被困在f = open(input_file, 'r')行上,说找不到我的sowpods.txt文件:(

2b085c9171bbae385f585a7eeb25d9e0.png

现在当我从终端尝试代码时,它将实际运行!:(不过,我正在为我的项目建立一个接口,所以希望尽快离开终端。在

1dfd0beeb1961f7c043c5cbb8a6b3a41.png

文本文件在正确的位置

0d17655b8c8612d4c5b206649044414d.png

问题代码,有什么想法吗?在from __future__ import print_function

from flask import Flask

import string

import sys

import sqlite3 as sqlite

app = Flask(__name__)

def test_for_db():

# test for existance of sowpods database

pass

def test_for_sowpods():

# test for existence of sowpods text file

pass

def word_score(input_word):

# score the word

# need to account for the blanks

scores = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,

"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,

"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,

"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,

"x": 8, "z": 10}

word_score = 0

for letter in input_word:

word_score = word_score + scores[letter]

return word_score

def word_list(input_file):

# create a list of tuples which containing the word, it's length, score and sorted value

sp_list = []

f = open(input_file, 'r')

for line in f:

sp_word = line.strip().lower()

sp_list.append((sp_word, len(sp_word), ''.join(sorted(sp_word)), word_score(sp_word)))

f.close()

return sp_list

def load_db(data_list):

# create database/connection string/table

conn = sqlite.connect("sowpods.db")

cursor = conn.cursor()

# create a table

tb_create = """CREATE TABLE spwords

(sp_word text, word_len int, word_alpha text, word_score int)

"""

conn.execute(tb_create)

conn.commit()

# Fill the table

conn.executemany("insert into spwords(sp_word, word_len, word_alpha, word_score) values (?,?,?,?)", data_list)

conn.commit()

# Print the table contents

for row in conn.execute("select sp_word, word_len, word_alpha, word_score from spwords"):

print (row)

if conn:

conn.close()

def print_help():

""" Help Docstring"""

pass

def test():

""" Testing Docstring"""

pass

if __name__=='__main__':

# test()

sp_file = "sowpods.txt"

load_db(word_list(sp_file))

app.run(debug=True)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值