简体   繁体   中英

Rename file names from file1..file1000 to file1001...file2000 using python

I found it difficult to rename the files. I have a scenario where I have to change a thousand files from filenames file1..file1000 to file1001..file2000 . I am using Linux OS and trying to change file names using a python for loop. I could concatenate the file names using the command below, but couldn't change the file names sequentially.

for i in file*; do mv -i "${i}" "${i/file/file100}" ; done

You could use shutil.move() :

import shutil

for i in range(1, 1001):
    shutil.move("file{}".format(i), "file{}".format(1000 + i))

或者只是坚持使用bash并使用相同的逻辑:

for i in {1..1000}; do mv file${i} file$(( $i + 1000 )); done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM