简体   繁体   中英

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.

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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