You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python读取CSV文件时遭遇PermissionError权限拒绝问题求助

解决PermissionError: [Errno 13] 权限拒绝问题

Hey there, let's work through this permission error you're hitting when trying to save your processed CSV without headers. First, let's fill in the gaps from your code snippet — I'm guessing you ended with a line like clean_X.to_csv('X_Data.csv', header=False) when the error popped up. Let's break down the possible fixes:

常见原因及对应解决方案

1. 文件正被其他程序锁定

This is the most frequent culprit: if you have X_Data.csv open in Excel, WPS, Notepad, or any other app, that program will lock the file to prevent edits from multiple sources. Python can't write to a locked file.

  • Fix: Close every program that has X_Data.csv open, then re-run your code.

2. No write permission for the target folder

If you just use the filename X_Data.csv, Python tries to save it in your script's current working directory. If that directory is a protected system folder (like Program Files on Windows) or a folder you don't have access to, you'll get this error.

  • Fixes:
    • Save the file to a folder you own, like your desktop:
      # Replace with your actual desktop path
      clean_X.to_csv(r'C:\Users\YourUsername\Desktop\X_Data.csv', header=False)
      
    • Check your current working directory first to confirm you have write access:
      import os
      print(os.getcwd())  # Prints the folder your script is running in
      

3. Filename/path has special characters or restrictions

Weird characters, spaces, or system-protected folder names can also block write access.

  • Fix: Simplify the filename (e.g., use clean_X.csv instead of something with special symbols) and save to a regular user folder (avoid system directories).

4. Double-check your save code (to remove headers properly)

Make sure you're using the right parameters to skip headers when saving. You probably also want to skip the DataFrame index to keep your file clean:

clean_X.to_csv('X_Data.csv', header=False, index=False)

Full Working Code Example

Here's your code with the missing save steps added correctly:

import pandas as pd
import numpy as np

# Load and clean raw data
df = pd.read_csv('input_doc.csv').replace(' ?', np.nan).dropna()
data_X = df.iloc[:, 1:15].values
data_Y = df.iloc[:, :1].values

# Convert arrays back to DataFrames
clean_X = pd.DataFrame(data_X)
clean_Y = pd.DataFrame(data_Y)

# Save without headers (and no index)
clean_X.to_csv('X_Data.csv', header=False, index=False)
clean_Y.to_csv('Y_Data.csv', header=False, index=False)

内容的提问来源于stack exchange,提问作者Sarasa Gunawardhana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:25:17