Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

python - How to increment numbers in filenames for multiple files without overwriting?

I was trying to rename the files using Python, but I got an unexpected result. I was renaming 10 files but in the end I got only 5 files.

How to rename files correctly using Python?

import os

start = 0
end  = 10
increment = 5 # This should work for 5 or 50 both

folder = 'catalog'
z = 1.5

# create file
if not os.path.exists('catalog'):
    os.mkdirs('catalog')

for i in range(start,end):
    with open(f'catalog/catalog_z1.5_{i:03d}.txt','w') as fo:
        fo.write(str(i))

# rename files
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)
    print(f2)
    print()
    os.rename(f,f2)

Note:

Here I have 10 files. The print shows correct file names, but when I look at the Finder there are only five files. How to fix the problem?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...