Look at what's happening:
start = 0
end = 10
increment = 5 # This should work for 5 or 50 both
folder = 'catalog'
z = 1.5
for i in range(start,end+1,1):
f = '{}/{}_z{:.1f}_{:03d}.txt'.format(folder,folder,z,i)
f2 = '{}/{}_z{:.1f}_{:03d}.txt'.format(folder,folder,z,i+increment)
print(f'{f} -> {f2}')
Output:
catalog/catalog_z1.5_000.txt -> catalog/catalog_z1.5_005.txt
catalog/catalog_z1.5_001.txt -> catalog/catalog_z1.5_006.txt
catalog/catalog_z1.5_002.txt -> catalog/catalog_z1.5_007.txt
catalog/catalog_z1.5_003.txt -> catalog/catalog_z1.5_008.txt
catalog/catalog_z1.5_004.txt -> catalog/catalog_z1.5_009.txt
catalog/catalog_z1.5_005.txt -> catalog/catalog_z1.5_010.txt
catalog/catalog_z1.5_006.txt -> catalog/catalog_z1.5_011.txt
catalog/catalog_z1.5_007.txt -> catalog/catalog_z1.5_012.txt
catalog/catalog_z1.5_008.txt -> catalog/catalog_z1.5_013.txt
catalog/catalog_z1.5_009.txt -> catalog/catalog_z1.5_014.txt
catalog/catalog_z1.5_010.txt -> catalog/catalog_z1.5_015.txt
So, following for example the 000.txt
file, it first gets renamed into 005.txt
(clobbering the original 005.txt
file). Then it becomes 010.txt
, etc.
How to fix it
In your specific case, you can go in reverse, since the renamed file is always higher (i+increment
) than the original: replace your for loop into for i in reversed(range(start, end+1)):
However, in the general case, it is advisable to clearly distinguish original files from transformed (or here, renamed) files to ensure the names don't collide (and that you are not confused by the state if the process fails in the middle). For example, you could name your original files: catalog/orig_catalog_z1.5_{i:03d}.txt
and the destination catalog/catalog_z1.5_{i:03d}.txt
, or have them in different directories.
Another way, if that's not an option, is to first rename them into a set of temp files:
import tempfile
prefix = tempfile.mktemp(prefix='.', dir=folder)
src, tmp, dst = zip(*[
(f'{folder}/{folder}_z{z:.1f}_{i:03d}.txt',
f'{prefix}-{folder}_z{z:.1f}_{i:03d}.txt',
f'{folder}/{folder}_z{z:.1f}_{i+increment:03d}.txt')
for i in range(start, end+1)
])
for a, b in zip(src + tmp, tmp + dst):
print(f'{a} -> {b}')
os.rename(a, b)
Output:
catalog/catalog_z1.5_000.txt -> catalog/.ns1opqyv-catalog_z1.5_000.txt
catalog/catalog_z1.5_001.txt -> catalog/.ns1opqyv-catalog_z1.5_001.txt
catalog/catalog_z1.5_002.txt -> catalog/.ns1opqyv-catalog_z1.5_002.txt
catalog/catalog_z1.5_003.txt -> catalog/.ns1opqyv-catalog_z1.5_003.txt
catalog/catalog_z1.5_004.txt -> catalog/.ns1opqyv-catalog_z1.5_004.txt
catalog/catalog_z1.5_005.txt -> catalog/.ns1opqyv-catalog_z1.5_005.txt
catalog/catalog_z1.5_006.txt -> catalog/.ns1opqyv-catalog_z1.5_006.txt
catalog/catalog_z1.5_007.txt -> catalog/.ns1opqyv-catalog_z1.5_007.txt
catalog/catalog_z1.5_008.txt -> catalog/.ns1opqyv-catalog_z1.5_008.txt
catalog/catalog_z1.5_009.txt -> catalog/.ns1opqyv-catalog_z1.5_009.txt
catalog/catalog_z1.5_010.txt -> catalog/.ns1opqyv-catalog_z1.5_010.txt
catalog/.ns1opqyv-catalog_z1.5_000.txt -> catalog/catalog_z1.5_005.txt
catalog/.ns1opqyv-catalog_z1.5_001.txt -> catalog/catalog_z1.5_006.txt
catalog/.ns1opqyv-catalog_z1.5_002.txt -> catalog/catalog_z1.5_007.txt
catalog/.ns1opqyv-catalog_z1.5_003.txt -> catalog/catalog_z1.5_008.txt
catalog/.ns1opqyv-catalog_z1.5_004.txt -> catalog/catalog_z1.5_009.txt
catalog/.ns1opqyv-catalog_z1.5_005.txt -> catalog/catalog_z1.5_010.txt
catalog/.ns1opqyv-catalog_z1.5_006.txt -> catalog/catalog_z1.5_011.txt
catalog/.ns1opqyv-catalog_z1.5_007.txt -> catalog/catalog_z1.5_012.txt
catalog/.ns1opqyv-catalog_z1.5_008.txt -> catalog/catalog_z1.5_013.txt
catalog/.ns1opqyv-catalog_z1.5_009.txt -> catalog/catalog_z1.5_014.txt
catalog/.ns1opqyv-catalog_z1.5_010.txt -> catalog/catalog_z1.5_015.txt