0

Say I have a folder with 1000 csv files which names are event_1.csv, event_2.csv,..., event_1000.csv.

I in fact have 25 folders like this and want to rename those files in such a way that the first 4 characters are 0001 for the first folder, 0002 for the second, all the way up to 0025. The last 4 characters represent the event, such that the 1st event is 0001, the second 0002, all the way to 1000.

So the 1st file in the 1st folder is changed in this fashion: event_1.csv = 00010001.csv.

Anyway my code is wrong, in that the first 100 files in the 1st folder are named 00020000.csv to 00020099.csv, since 0002 should be used in the 2nd folder only. Then, from the 101st file to the last, I get the correct filenames: 00010101.csv to 00011000.csv.

This is my code: what is wrong with that?

import os, sys
import glob
import csv

directory=r'C:\Users\MyName\Desktop\Tests'
subdir=[x[0] for x in os.walk(directory)]
subdir.pop(0)

N=['0001','0002','0003','0004','0005','0006','0007','0008','0009','0010','0011','0012','0013','0014','0015','0016','0017','0018','0019','0020','0021','0022','0023','0024','0025']

for i in subdir:
    for n in N:
        temp_dir=r''+i
        os.chdir(temp_dir)
        A=str(n)
        for file in glob.glob("*.csv"):
            if len(file)==11:
                event='000'+str(file[6])
                newname=A+event
                os.rename(file, newname + '.csv')
            if len(file)==12:
                event='00'+str(file[6:8])
                newname=A+event
                os.rename(file, newname + '.csv')
            if len(file)==13:
                event='0'+str(file[6:9])
                newname=A+event
                os.rename(file, newname + '.csv')
            if len(file)==14:
                event=file[6:10]
                newname=A+event
                os.rename(file, newname + '.csv')
FaCoffee
  • 7,609
  • 28
  • 99
  • 174
  • 1
    You might want to look into [zero-padding](http://stackoverflow.com/questions/339007/nicest-way-to-pad-zeroes-to-string) to simplify your code – M.T Jul 28 '16 at 07:48

1 Answers1

1

If you're sure about all the names of your files, you could considerably simplify your code (as M.T said). Try maybe something like :

for n,i in enumerate(subdir):
        os.chdir(r''+i)   # Or whatever your folders are named
        for m,file in enumerate(glob.glob("*.csv")):
            newname = "{0:04d}{1:04d}.csv".format(n+1,m+1)
            os.rename(file, newname)

EDIT : it's better with enumerate.

Daneel
  • 1,173
  • 4
  • 15
  • 36
  • `subdir` is the list of all the 25 folders, which are located in a mother folder. – FaCoffee Jul 28 '16 at 08:09
  • 1
    This will ignore the current name of files and just rename in whatever order glob.glob() return them. – polku Jul 28 '16 at 08:13
  • 1
    perhaps use `enumerate` instead of counters at the cost of writing `n+1` in `format`? and also `n++` is not python syntax, instead `n+=1`. – M.T Jul 28 '16 at 08:15
  • You're right, it seems "natural" that the files come in the lexical order but it is not certain... Do you have a trick to fix that ? – Daneel Jul 28 '16 at 08:15
  • @Daneel one could parse the filenames. `m=int(filename[6:-4])` – M.T Jul 28 '16 at 08:18
  • IDK maybe regex to match the digit group and padding it. – polku Jul 28 '16 at 08:18