CF84 CF84 - 4 months ago 4
Python Question

Python: renaming files returns the wrong name

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')

Answer

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.

Comments