0

I have some csv files in a folder and I am trying to delete all blank rows and move the news files into a new folder.

Here is the code I have:

import csv
import glob
import os
import shutil

path = 'in_folder/*.csv'
files=glob.glob(path)

#Read every file in the directory

x = 0 #counter

for filename in files:

    with open(filename, 'r') as fin:
        data = fin.read().splitlines(True)

        with open(filename, 'w') as fout:
            for line in fin.readlines():
                if ''.join(line.split(',')).strip() == '':
                    continue
                fout.write(line)
            x += 1            


dir_src = "in_folder"
dir_dst = "out_folder"


for file in os.listdir(dir_src):
    if x>0:
        src_file = os.path.join(dir_src, file)
        dst_file = os.path.join(dir_dst, file)
        shutil.move(src_file, dst_file)

What the code is doing right now is deleting everything from the files and moving them to the new folder. I want my files to be the same but with deleted blank rows.

5
  • So do you want to modify the original? Delete the original? Commented Nov 28, 2018 at 12:10
  • Or in Perl (example is for single file) !! - perl -ni.old -e 'print unless /^\s*$/' file (stackoverflow.com/a/8270397/1755628) Commented Nov 28, 2018 at 12:32
  • @PeterWood Delete the empty rows, and put the new file into a new folder. Commented Nov 28, 2018 at 12:48
  • You're not being precise. Do you want the original file gone? If not, do you want the original file modified? Commented Nov 28, 2018 at 12:50
  • I want the original file modified. Commented Nov 28, 2018 at 13:18

2 Answers 2

1

You can just output every line to the new file, no need to do any moving afterwards:

dir_src = "in_folder/*.csv"
dir_dst = "out_folder"

files = glob.glob(dir_src)

# Read every file in the directory

x = 0 # counter

for filename in files:
    outfilename = os.path.join(dir_dst, os.path.basename(filename))
    with open(filename, 'r') as fin:
        with open(outfilename, 'w') as fout:
            for line in fin:
                if ''.join(line.split(',')).strip() == '':
                    continue
                fout.write(line)
            x += 1            
Sign up to request clarification or add additional context in comments.

Comments

0

try this.

for filename in files:
    with open(filename, 'r') as fin:
        data = fin.read().splitlines(True)
        with open(filename, 'w') as fout:
            for line in data:
                if ''.join(line.split(',')).strip() == '':
                    continue
                fout.write(line)
            x += 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.