【问题标题】:PermissionError: [Errno 13] Permission denied in python jupyter notebookPermissionError:[Errno 13] python jupyter notebook 中的权限被拒绝
【发布时间】:2020-11-21 23:45:18
【问题描述】:

我正在尝试构造文件名及其路径,然后读取该文件,但它给出了这个错误。

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"]

出现以下错误:

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'

【问题讨论】:

  • 试试这个! stackoverflow.com/a/13207548/11323304 如果这不起作用,请回复。
  • 最好提供绝对路径或相对路径
  • @zerecees- 我没有打开文件,我正在加载目录。它仍然不适合我。

标签: python jupyter-notebook errno


【解决方案1】:

os.listdir("clean_data/") 为您提供一个由包含在"clean_data/" 文件夹中的文件和目录组成的列表,并且尝试使用open() 命令打开一个目录会抛出您得到的错误,您只能打开文件。

因此,在"clean_data/" 文件夹中,您似乎还有一些目录,并且您正试图(无意地)打开其中一个,因为它是一个文件。

如果您只对 "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()

【讨论】:

    猜你喜欢
    • 2013-05-31
    • 1970-01-01
    • 2015-07-17
    • 2016-07-25
    • 2018-11-18
    • 1970-01-01
    • 2020-07-01
    • 2016-11-12
    • 2019-11-09
    相关资源
    最近更新 更多