如何修复 python 中的权限被拒绝错误?

How to fix permission denied error in python?

我试图创建一个点文件来可视化我的决策树,但出现权限被拒绝的错误

credit_df=pd.read_csv('credit.csv', index_col=0) #importing the dataset
X = credit_df.drop("default" , axis=1) #defining independent variable
Y=credit_df.pop("default") #defining dependent variable

from sklearn.model_selection import train_test_split #spliting the data into train and test data

X_train, X_test, train_labels, test_labels = train_test_split(X, y, test_size=.30, random_state=1) #categorising the data

dt_model = DecisionTreeClassifier(criterion = 'gini' ) #creating the decision tree model

dt_model.fit(X_train,train_labels) #fitting the model into the train data

from sklearn import tree
train_char_label=['NO','YES'] #defining the target variable label
credit_tree_title=open('c:\credit_tree.dot','w') #creating the dot file
dot_data = tree.export_graphviz(dt_model, out_file=Credit_Tree_File, feature_names = list(X_train), class_names = list(train_char_label))

Credit_Tree_File.close() #closing the dot file

我按照 Tim Roberts 的建议使用了带双斜杠的文件夹路径而不是 c 驱动器,我的问题得到了解决。

from sklearn import tree
train_char_label=['NO','YES']
credit_tree_file=open('C:\Users\jyoti\Desktop\Jupyter Notebook\Data Mining\CART\Case study\Credit\credit_tree.dot' , 'w')
dot_data = tree.export_graphviz(dt_model, out_file=credit_tree_file, feature_names = list(X_train), class_names = list(train_char_label))

credit_tree_file.close()