【问题标题】:Pandas IOError: [Errno 13] Permission deniedPandas IOError:[Errno 13] 权限被拒绝
【发布时间】:2017-11-05 16:46:02
【问题描述】:

我一直在尝试在 macbook pro 上使用 python 2.7 运行 pandas 并不断收到以下错误:

文件“/Users/Hofstadter/anaconda/lib/python2.7/site-packages/pandas/io/common.py”,第 376 行,在 _get_handle f = open(path_or_buf, mode)

IOError: [Errno 13] Permission denied: 'datasets/cats_0.8_0.6_0.4_0.2/target.csv'

似乎出于某种原因,为以下文件(包括 target.csv)创建的文件夹具有受限权限。这段代码是这样的:

def get_tables(df):
    categorical_cols = [col for col in df.columns if col.endswith('_cat')]
    train_table = df[categorical_cols]
    for col in categorical_cols:
        train_table = pd.concat(
            [
                train_table, pd.get_dummies(
                    train_table[col],
                    prefix=col,
                    prefix_sep='_',
                    dummy_na=False).astype(int)
            ],
            axis=1,
            join='inner')
        train_table.drop(col, axis=1, inplace=True)

    print('Tables Created :)')
    return train_table

这些表的创建没有问题,但是当我尝试按如下方式保存它们时出现权限错误。

def save_tables(data_path,
                df,
                top_quant,
                mh_quant,
                ml_quant,
                low_quant,
                train=True):

    df = categorize_features(df, top_quant, mh_quant, ml_quant, low_quant)
    X = get_tables(df)

    os.makedirs(data_path, True)

    x_path = '{}/tournament_table.csv'.format(data_path)

    if train:
        x_path = '{}/train_table.csv'.format(data_path)
        y = df['target'].to_frame()
        y.columns = ['target']
        y.to_csv('{}/target.csv'.format(data_path), index=False)
    else:
        ids = df['id'].to_frame()
        ids.columns = ['id']
        ids.to_csv('{}/ids.csv'.format(data_path), index=False)

    X.to_csv(x_path, index=False)

【问题讨论】:

  • 出现错误是因为您尝试使用一些您没有权限的文件,而不是因为/Users/Hofstadter/anaconda/lib/python2.7/site-packages/pandas/io/common.py 没有正确的权限。
  • @juanpa.arrivillaga 嗯,好吧,但为什么我不能访问标准 pandas 库的一部分?有没有办法可以重新安装熊猫,以便我拥有正确的权限?如果有帮助,我正在使用 Anaconda。
  • 如果您是Hofstadter 并且该文件属于您,那么您必须使用u+ra+r,而不是o+r,以使其可读。但是mode的值是多少?
  • 不,你没听懂我的意思,我是说问题不在于 pandas 源,而是您尝试使用 pandas 打开的某个文件.
  • 请显示完整错误信息。

标签: python pandas scikit-learn sklearn-pandas


【解决方案1】:

我怀疑您的问题不是电话to_csv(),而是您拨打的电话os.makedirs

调用os.makedirs(data_path, True) 将True 解释为模式的参数。见:

makedirs(name [, mode=0o777][, exist_ok=False])

如果你想对新目录使用默认模式但忽略现有目录,你的电话应该是os.makedirs(data_path, exist_ok=True)

【讨论】:

  • 谢谢,但如果我进行更改,我会收到以下错误:文件“get_dataset.py”,第 62 行,在 save_tables os.makedirs(data_path,exist_ok=True) TypeError: makedirs() got意外的关键字参数“exist_ok”
  • 我明白了。您正在使用 Python 2.7。 os.makedirs() 不同。请参阅docs。只需像os.makedirs(data_path) 一样调用它,但如果目录已经存在,您将不得不手动处理异常情况。 exist_ok 选项仅在 Python 3 中可用。
  • 非常感谢,我应该看看那个。所以现在令人惊讶的是,该文件是在没有任何权限问题的情况下创建的。但是我现在收到以下错误(我已经确认该文件夹在运行程序之前不存在并且之后确实存在):File "/Users/Hofstadter/anaconda/lib/python2.7/os.py", line 157, in makedirs mkdir(name, mode)OSError: [Errno 17] File exists: '/Users/Hofstadter/path/cats_0.8_0.6_0.4_0.2'
猜你喜欢
  • 1970-01-01
  • 2011-06-11
  • 2013-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多