-2

Trying to convert multiple (5) CSVs to TSVs using python, but when I run this it only creates 1 TSV. Can anyone help?

import csv
import sys
import os
import pathlib

print ("Exercise1.csv"), sys.argv[0]

dirname = pathlib.Path('/Users/Amber/Documents')

for file in pathlib.Path().rglob('*.csv'):

    with open(file,'r') as csvin, open('Exercise1.tsv', 'w') as tsvout:
        csvin = csv.reader(csvin)
        tsvout = csv.writer(tsvout, delimiter='\t')

        for row in csvin:
            print(row)
            tsvout.writerow(row)
    exit ()

Thanks!

amb
  • 27
  • 6
  • 3
    Look at the second `open` in your `with` statement... it always uses the same name. – Mark Setchell Feb 09 '20 at 15:27
  • 2
    Furthermore, `exit()` is called at the end of the first iteration. – mkrieger1 Feb 09 '20 at 15:28
  • @mkrieger1: If I move ```exit()``` one indentation to the left, it begins to run an infinite loop. Any idea why this is happening? – amb Feb 09 '20 at 19:44
  • What if you print the `file` variable in each iteration of the `for file in pathlib.Path()...` loop to see what is happening? – mkrieger1 Feb 09 '20 at 19:58
  • @mkrieger1: would i do that by adding ```print(i,file)``` one line under ```for i,file in pathlib.Path()...``` ? sorry perhaps a basic question, i'm very new to this! – amb Feb 10 '20 at 00:29
  • You can answer this yourself by trying it! – mkrieger1 Feb 10 '20 at 14:44

1 Answers1

1

You're opening each file in the .csv folder with your for loop, but only opening a single file to write to (Exercise1.tsv). So you're overwriting the same file each time. You need to make new files to write to in each iteration of the loop. You could try something like this:

for i,file in enumerate(pathlib.Path().rglob('*.csv')):

    with open(file,'r') as csvin, open('Exercise_{}.tsv'.format(i), 'w') as tsvout:
        csvin = csv.reader(csvin)
        tsvout = csv.writer(tsvout, delimiter='\t')

enumerate() adds a counter to the for loop. This will append a number to your Exercise.tsv files from 0 to the length of the files in your directory.

mhdev
  • 166
  • 1
  • 10
  • thanks for the help! I tried that, and it does create a new TSV titled 'Exercise_0' as opposed to Exercise1, but still only 1 TSV file is created...and when I tried moving the ```exit()``` statement to the left, it begins creating a ton of TSV (many more files than CSVs that I have in that directory?!)...do you know why this could be happening? @dayno85 – amb Feb 09 '20 at 19:46