Python [Errno 13] 权限被拒绝:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40984791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python [Errno 13] Permission denied:
提问by digital_alchemy
I'm attempting to write a quick python script to iterate through all csv files in the current folder and remove the header row from them then store them in a separate folder.
我正在尝试编写一个快速的 python 脚本来遍历当前文件夹中的所有 csv 文件,并从中删除标题行,然后将它们存储在一个单独的文件夹中。
The current working directory has four sample csv files and the python script. Once executed the script creates the HeaderRemoved directory.
当前工作目录有四个示例 csv 文件和 python 脚本。一旦执行,脚本将创建 HeaderRemoved 目录。
It appears that once the folder is created the code that is attempting to read the files is trying to access the folder but looking at the code I'm not sure why it would be.
似乎一旦创建了文件夹,尝试读取文件的代码就会尝试访问该文件夹,但查看代码时我不确定为什么会这样。
I'm on a windows machine at the moment.
我现在在 Windows 机器上。
import csv, os, argparse, string
from ctypes import *
os.makedirs('HeaderRemoved', exist_ok=True)
# Loop through files in the current working directory
for csvFile in os.listdir('.'):
if not csvFile.endswith('.csv'):
continue # Skips non-csv files
print ('Removing header from ' + csvFile + '...')
# Read in CSV skipping the first row
csvRows = []
csvFileObj = open(csvFile)
csvReader = csv.reader(csvFileObj)
for row in csvReader:
if csvReader.line_num == 1:
continue # Skips the first row
csvRows.append(row)
csvFileObj.close()
# Write out the CSV file
csvFileObj = open (os.path.join('HeaderRemoved', csvFile), 'w', newline='')
for row in csvRows:
csvWriter.writerow(row)
csvFileObj.close()
Sample output:
示例输出:
Removing header from examplefile_1.csv...
Removing header from examplefile_2.csv...
Removing header from examplefile_3.csv...
Removing header from examplefile_4.csv...
Traceback (most recent call last): File "atbs_csv_parse.py", line 14, in <module>
csvFileObj = open(csvFile) PermissionError: [Errno 13] Permission denied: 'HeaderRemoved'
采纳答案by digital_alchemy
As Charles Duffy commented under my original question, the issue was in fact that the lines of code for reading and writing the files had not been indented to fall within the for loop. Correcting the indentation fixed the issue and it now works as desired.
正如查尔斯·达菲 (Charles Duffy) 在我最初的问题下评论的那样,问题实际上是用于读取和写入文件的代码行没有缩进以落入 for 循环。更正缩进修复了问题,现在它可以按需要工作。
A good reminder to always check the simple things.... I got so wrapped up in why it wasn't working that I didn't even notice the lack of indentation.
一个很好的提醒,总是检查简单的事情......我对为什么它不起作用以至于我什至没有注意到缺少缩进感到如此困惑。
回答by Chanaka Fernando
In my case, I had opened the csv file via Excel and ran the script. Then this Permission denied exception occurred.
就我而言,我通过 Excel 打开了 csv 文件并运行了脚本。然后这个 Permission denied 异常发生了。
Just closed the opened file and run the script again :)
刚刚关闭打开的文件并再次运行脚本:)
回答by Randolfo
In my case, the same error was because I was passing a directory name instead the file name.
就我而言,同样的错误是因为我传递的是目录名而不是文件名。
Maybe could be the same problem of others.
也许其他人也有同样的问题。
回答by Ashwani Kumar
The file in script is opened somewhere in the system.That is the reason for getting PermissionError : [Error 13]. Solution : Just close the file and run the script. You won't get the error.
脚本中的文件在系统中的某处打开。这就是获取 PermissionError 的原因:[错误 13]。解决方案:只需关闭文件并运行脚本。你不会得到错误。