如何解决,PermissionError:[Errno 13]权限被拒绝:'test.from'

时间:2019-05-30 22:08:11

标签: python pandas sqlite

我正在关注有关训练python聊天机器人数据的教程。但是,每当我运行代码时,都会出现此错误,

line 22, in <module>
    with open('test.from','a', encoding='utf8') as f:
PermissionError: [Errno 13] Permission denied: 'test.from'

我转到我的项目文件夹,并更改了“工作人员”和“所有人”的权限。我做到了,以便两者都可以读写。但这什么也没做,我仍然遇到同样的错误。

下面是我的代码,

import sqlite3
import pandas as pd

timeframes = ['2015-01']

for timeframe in timeframes:
    connection = sqlite3.connect('/Users/usr/Desktop/fileName/RC_{}.db'.format(timeframe))
    c = connection.cursor()
    limit = 5000
    last_unix = 0
    cur_length = limit
    counter = 0
    test_done = False

    while cur_length == limit:
        df = pd.read_sql("SELECT * FROM parent_reply WHERE unix > {} and parent NOT NULL and score > 0 ORDER BY unix ASC LIMIT {}".format(last_unix,limit),connection)
        last_unix = df.tail(1)['unix'].values[0]
        cur_length = len(df)

        if not test_done:
            with open('test.from','a', encoding='utf8') as f:
                for content in df['parent'].values:
                    f.write(content+'\n')
            with open('test.to','a', encoding='utf8') as f:
                for content in df['comment'].values:
                    f.write(str(content)+'\n')
            test_done = True

1 个答案:

答案 0 :(得分:1)

可能的原因

  1. 用户在该目录中没有写权限。

  2. 该文件受写保护。

  3. 该文件正在其他来源使用。

很可能您在该目录中没有写访问权,因此请输入您有权访问的目录的完整路径,然后重试。