亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用重命名功能

如何使用重命名功能

慕沐林林 2021-09-25 13:20:08
這是我得到的錯誤:系統找不到指定的文件:'1.jpg' -> '0.jpg'即使我在目錄中有一個名為 1.jpg 的文件。我正在制作文件重命名腳本,該腳本重命名給定的目錄中的所有文件,該數字隨每個文件增加 +1。import osdef moving_script():directory = input("Give the directory")xlist = os.listdir(directory)counter = 0for files in xlist:    os.rename(files, str(counter)+".jpg")    counter = counter + 1moving_script()它應該將所有文件重命名為“0.jpg”、“1.jpg”等
查看完整描述

2 回答

?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

代碼:


import os



def moving_script():

    directory = input("Give the directory")

    xlist = os.listdir(directory)

    counter = 0


    for files in xlist:

        os.rename(os.path.join(directory, files),

                  os.path.join(directory, str(counter)+".jpg"))

        counter = counter + 1



if __name__ == '__main__':

    moving_script()

結果:


~/Documents$ touch file0 file1 file2 file3 file4



ls ~/Documents/

file0  file1  file2  file3  file4



$ python renamer.py

Give the directory'/home/suser/Documents'


$ ls ~/Documents/

0.jpg  1.jpg  2.jpg  3.jpg  4.jpg


查看完整回答
反對 回復 2021-09-25
  • 2 回答
  • 0 關注
  • 198 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號