简体   繁体   English

PermissionError:[Errno 13] python jupyter notebook 中的权限被拒绝

[英]PermissionError: [Errno 13] Permission denied in python jupyter notebook

I am trying to construct filename and its path, and then reading that file but it is giving this error.我正在尝试构造文件名及其路径,然后读取该文件,但它给出了这个错误。

import os
import pandas as pd
all_text_samples = []
# file_list contains names of all files in "clean_data" folder
file_list = os.listdir("clean_data/")

for file_name in file_list:
# Construct filename and its path
file = (f"clean_data/" + file_name)

# Now open file for reading
my_text_file = open(file, encoding="utf8")
file_data = my_text_file.read()

# Append the data to the list
all_text_samples.append(file_data)

# Convert list to dataframe
text_dataframe = pd.DataFrame(all_text_samples)
text_dataframe.columns = ["Text"]

It is giving the following error:它给出以下错误:

PermissionError                           Traceback (most recent call last)
<ipython-input-4-8e06886c8a51> in <module>
      4 
      5     # Now open file for reading
----> 6     my_text_file = open(file, encoding="utf8")
      7     file_data = my_text_file.read()
      8 

PermissionError: [Errno 13] Permission denied: 'clean_data/clean_data'

os.listdir("clean_data/") gives you a list composed of either files and directories contained in "clean_data/" folder and trying to open a directory with open() command throws the error that you get, you can only open files. os.listdir("clean_data/")为您提供由"clean_data/"文件夹中包含的文件和目录组成的列表,尝试使用open()命令打开目录会引发错误,您只能打开文件。

So, it seems that in "clean_data/" folder you have also some dirctories and that you are trying (unintentionally) to open one of them as it was a file.因此,似乎在"clean_data/"文件夹中还有一些目录,并且您正试图(无意中)打开其中一个,因为它是一个文件。

If you are interested only in files contained in "clean_data/" folder you could easily modify your code to filter out directories and open only files:如果您只对"clean_data/"文件夹中包含的文件感兴趣,您可以轻松修改代码以过滤掉目录并仅打开文件:

# Now open file for reading
if not os.path.isdir(file):
    my_text_file = open(file, encoding="utf8")
    file_data = my_text_file.read()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM