-1

I have a list of files I iterate through to merge them all into one .csv file:

with open('C:\\TODAY.csv', 'w') as f_obj:
    rows = []
    files = os.listdir('C:\RAW\\')
    for f in files:
        if fnmatch.fnmatch(f, '*.csv') and not fnmatch.fnmatch(f, 'TODAY.CSV'):
            print f
            rows.append(open(os.path.join('C:\\RAW_OTQ\\', f)).readlines())
    iter = izip_longest(*rows)
    for row in iter:
         f_obj.write(','.join([field.strip() for field in row if field is not None]) + '\n')

This works as intended, however it has the most recent date in the left most column. What I want is to have it reversed, so the oldest is read first.

This could be achieved if it starts the iteration at the end and works backwards, as this will just reverse the order in which they are appended to the list, and then the list will, in essence, be an exact reverse of what it is currently.

How would I go about reversing the order in which the files are read.

Please note: I don't want the individual files to be read backwards, but just reverse the order in which the files are read.

Joe Smart
  • 751
  • 3
  • 10
  • 28
  • possible duplicate of [Traverse a list in reverse order in Python](http://stackoverflow.com/questions/529424/traverse-a-list-in-reverse-order-in-python) – khelwood Sep 15 '14 at 12:22
  • Where's the date coming from? The content of the files in `RAW_OTQ`? Why are you listing files from `RAW` but then reading them from `RAW_OTQ`? – MattH Sep 15 '14 at 12:26
  • Sorry, both come from `RAW_OTQ` but just for confidentiality changed the name, must have missed one! And the date is the filename, in format `%d%m%y` eg. `100914.csv` – Joe Smart Sep 15 '14 at 12:33
  • you can create a reversed list using the following: `reversedlist = oldlist[::-1]` – NDevox Sep 15 '14 at 12:50
  • Also worth noting that if the dates are the first field in the row, and they follow the iso standard (yyyy-mm-dd), then `list.sort(reverse=True)` should also work. – NDevox Sep 15 '14 at 12:51

1 Answers1

1
>>> values = [2, 4, 6, 8, 10, 12, 14]
>>> for x in reversed(values):
...     print(x)
... 
14
12
10
8
6
4
2
vz0
  • 32,345
  • 7
  • 44
  • 77