PermissionError:[Errno 13]权限被拒绝:'emails2 / Train'

问题描述 投票:1回答:1

我曾在stackoverflow和其他网站上进行搜索,但似乎仍未找到解决方案。我的问题是,我正在尝试访问两个包含“火腿”或“垃圾邮件”电子邮件的不同文件夹,以将它们放入数据集以进行模型训练。我似乎总是收到权限错误,并且不确定如何通过Python或Windows资源管理器解决它。我想知道如何以多种方式解决它,以更好地理解它。

这里是代码:

ham = 'ham'
spam = 'spam'
data = 'emails2'
hamfiles = []
spamfiles = []

'''Searching File Path'''
print('# MESSAGE: Finding for files ----------------------------------------------------------------------------------')
for subdir, folders, files in os.walk(data):
    if subdir.__contains__(ham):
        # print(subdir)
        for file in files:
            # print(os.path.join(subdir, file))
            hamfiles.append(os.path.join(subdir, file))
    else:
        for file in files:
            # print(os.path.join(subdir, file))
            spamfiles.append(os.path.join(subdir, file))

import glob

X_file = []
y_class = []

eof = [('eof')]

for hamfile in hamfiles:
    # print(hamfile)
    files = glob.glob(hamfile)
    for file in files:
        # print(file)
        h = open(file, encoding='UTF8', errors='replace')
        buffer = h.read()

        '''Tokenize'''
        token = nltk.word_tokenize(buffer)
        '''Part Of Speech Tagging'''
        posTag = nltk.pos_tag(token)
        '''Append to Array'''
        for (word, tag) in posTag:
            X_file.append(word)
            y_class.append('ham')


for spamfile in spamfiles:
    # print(spamfile)
    files = glob.glob(spamfile)
    for file in files:
        # print(file)
        s = open(file, encoding='UTF8', errors='replace')
        buffer = s.read()

        '''Tokenize'''
        token = nltk.word_tokenize(buffer)
        '''Part Of Speech Tagging'''
        posTag = nltk.pos_tag(token)
        '''Append to Array'''
        for (word, tag) in posTag:
            X_file.append(word)
            y_class.append('spam')

print('# MESSAGE: Print X_ham ----------------------------------------------------------------------------------------')
print(X_file)
h.close()

def create_lexicon(X_file,y_class):

    lexicon = []
    with open(X_file,'r+') as f:
        contents = f.readlines()
        for l in contents[:hm_lines]:
            all_words = word_tokenize(l)
            lexicon += list(all_words)

    with open(y_class,'r+') as f:
        contents = f.readlines()
        for l in contents[:hm_lines]:
            all_words = word_tokenize(l)
            lexicon += list(all_words)

我知道这可能是Windows权限错误,但我以前从未遇到过。在此先感谢!

python permissions operating-system permission-denied
1个回答
0
投票

如果您正在命令提示符下运行python文件。当您打开命令提示符时,右键单击并选择以管理员身份运行。

[如果您正在使用任何其他IDE(例如spyder pycharm等。),也请尝试以管理员身份运行它

© www.soinside.com 2019 - 2024. All rights reserved.