简体   繁体   中英

How rename files in a directory using python?

The python script I am writing should be able to work in any folder I put in. I have to find all the .avi files. If the file name ends with cropped.avi then I have to rename it and save it as .avi, without the word cropped.

import os
import glob

os.getcwd()
glob.glob('*.avi')
directory = glob.glob('*.avi')

for directory:
    if i.endswith("cropped.avi"):
         os.rename("cropped.avi",".avi")

I think my code is missing something and I don't know what to do!!

The for loop syntax was incorrect. This works:

import os
import glob

directory = glob.glob('*.avi')
for i in directory:
    if i.endswith("cropped.avi"):
         os.rename("cropped.avi",".avi")

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