Question

I have a txt file with roughly 1000 lines, i want to setup colum 1 as the soruce destination and column 2 as the transfer to destination and using shutil.copyfile(src, dst) copy these files.

I have it setup as a simple print (src, dst) right now so i can see whats going on. I'm not doing something right in the for src in vld to add in the linux value.

Am i way off with this current setup to get the value of src lined up with the corresponding value of dst ? I'm using Python 2.4

import csv
import operator
import sys
import os
import shutil

inf = csv.reader(open('data.txt','r'))
sortedlist = sorted(inf, key=operator.itemgetter(2), reverse=True)
def dothis(x):
        for row in sortedlist:
                print row[x]

vld = dothis(0)
linux = dothis(1)

for src in vld:
        dst = linux
        print (src, dst)

current error message:

    Traceback (most recent call last): File "test.py", line 16, in ? for src in vld:
TypeError: iteration over non-sequence  
Was it helpful?

Solution

You almost had it, but veered off-track somewhere there. :) dothis doesn't return anything, so vld and linux are both going to be empty.

csv.reader already "lines up" the values for you; it yields rows at a time. You're actually breaking them apart again when there's no need.

inf = csv.reader(open('data.txt','r'))
sortedlist = sorted(inf, key=operator.itemgetter(2), reverse=True)

for row in sortedlist:
    src = row[0]
    dst = row[1]
    print src, dst
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top