Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove CSV header
#2
I don't understand the issue, but csvFilename should not be __pycache__. I seems to me that lines 14-29 should be indented in the for loop.

That said, I would better try a shorter way using shutil.copyfileobj
#! python3
# removecsvheader.py - Removes the header from all CSV files in the current working directory
 
import csv, os
import shutil
 
os.makedirs('headerRemoved', exist_ok=True)
 
# loop through every file in the current working directory.
for csvFilename in os.listdir('.'):
    if not csvFilename.endswith('.csv'):
        continue # skip non-csv files
    print('Removing header from ' + csvFilename + '...')
    targetFilename = os.path.join('headerRemoved', csvFilename)
    with open(csvFilename) as ifo, open(targetFilename, "w") as ofo:
        ifo.readline()
        shutil.copyfileobj(ifo, ofo)
Reply


Messages In This Thread
Remove CSV header - by Truman - Dec-25-2018, 12:07 AM
RE: Remove CSV header - by Gribouillis - Dec-25-2018, 07:54 AM
RE: Remove CSV header - by Larz60+ - Dec-25-2018, 11:08 AM
RE: Remove CSV header - by Truman - Dec-26-2018, 12:30 AM
RE: Remove CSV header - by Gribouillis - Dec-26-2018, 06:31 AM
RE: Remove CSV header - by Truman - Dec-26-2018, 10:55 PM
RE: Remove CSV header - by snippsat - Dec-26-2018, 11:16 PM
RE: Remove CSV header - by Gribouillis - Dec-26-2018, 11:19 PM
RE: Remove CSV header - by snippsat - Dec-26-2018, 11:28 PM
RE: Remove CSV header - by Gribouillis - Dec-26-2018, 11:39 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020