windows10安装python显示errno 13,Python [Errno 13]权限被拒绝:

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.

The current working directory has four sample csv files and the python script. Once executed the script creates the HeaderRemoved directory.

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.

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

csvFileObj = open(csvFile) PermissionError: [Errno 13] Permission denied: 'HeaderRemoved'

解决方案

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.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值